Reputation:
As in SAS we can start multiple varibles using colon(:) option with start name. I wanted to do the same in R for modeling purpose.
Any suggestions?
Upvotes: 0
Views: 230
Reputation: 143
as.formula(paste("y~", paste(names(mydata)[substr(names(mydata), 1, 1)=="x"], collapse="+"))) -> myformula
gives a formula object myformula
for a regression of y
on all variables with names beginning with x
in the data frame mydata
that you can use in models, e.g. lm(myformula, data=mydata)
. So you're not sub-setting the data frame, which can be a nuisance when it's big.
Upvotes: 0
Reputation: 3562
There are probably many ways to do this. Here is one with a regular expression that doesn't do exactly what you want, but might do the trick:
x1 = rnorm(100)
x2 = rnorm(100)
z = rnorm(100)
a = rnorm(100)
y = x1+x2+z
d = data.frame(x1,x2,z,y)
X = as.matrix(d[,grepl("x",colnames(d))])
head(X)
m = lm(y~X+a)
summary(m)
Upvotes: 1