Three ways to increase flexibility.
Method 1
Run a regression using the formula notation:
fit <- lm( Y ~ . , data=dat )
Method 2
Put all your data in one data.frame file, not two:
dat <- cbind(data.frame(Y=Y),as.data.frame(X))
Then run the regression using the formula notation:
fit <- lm( Y~. , data=dat )
Method 3
Another way is to build the formula yourself:
model1.form.text <- paste("Y ~",paste(xvars,collapse=" + "),collapse=" ") model1.form <- as.formula( model1.form.text ) model1 <- lm( model1.form, data=dat )
In this example, xvars is a character vector containing the names of the variables you want to use.
source share