user49305
user49305

Reputation:

Selecting multiple variables into model starting with common name in R

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

Answers (2)

Scortchi
Scortchi

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

generic_user
generic_user

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

Related Questions