Durden Durden - 2 months ago 12
R Question

Defining global collection of variables in R

In Stata, one can define a global like

global PETS cats dogs rabbits mice
which collects these variables in some bucket named
PETS
. One can then use this in

reg happiness $PETS


which effectively runs
reg happiness cats dogs rabbits mice
. Is there an equivalent for this in R that allows something like
m <- lm(happiness ~ PETS + other_variable)
?

Answer

You can use this workaround:

PETS <- c("dogs", "rabbits", "mice")
m <- lm( as.formula( paste( "happiness ~ other_variable +", paste(PETS, collapse=" + ") ) ) )