Is there a way to load all variables starting with the same prefix in R (equivalent to stata's: reg y x*)
Shortcut variables in R
-
I'm not sure if there is literally something as easy as reg y x*. Depending on your data set, lm(y ~ ., data = mydata) might work. You can get rid of irrelevant variables by including a - sign.
also see this for further reference http://stackoverflow.com/questions/5251507/how-to-succinctly-write-a-formula-with-many-variables-from-a-data-frame
-
You can subset your data frame by the boolean vector that results from comparing your prefix to the trimmed character vector of variable names.
lm(Y~.,data["PREFIX"==strtrim(names(data),PREFIXLENGTH)])
But you're better off using regular expressions. For instance,
lm(Y~.,data[grepl("^X",names(data))])