Here is an (artificial) example.chamber_sizes <- mtcars$disp/mtcars$cyl form <- hp ~ chamber_sizes model <- lm(form, data = mtcars) print(model) # Call: # lm(formula = form, data = mtcars) # # Coefficients: # (Intercept) chamber_sizes # 2.937 4.104
Notice: one of the variables came from a vector in the environment, not from the primary
chamber_sizeswas first looked for in the
data.frame, and then in the environment the
formulawas defined (which happens to be the global environment), and (if that hadn’t worked) in the executing environment (which is again the global environment).
Our advice is: do not do that. Place all of your values in columns. Make it unambiguous all variables are names of columns in your
data.frameof interest. This allows you to write simple code that works over explicit data. The style we recommend looks like the following.
Read the whole thing.