This question already has an answer here:
It appears simple, but I don't know how to code it in R. I have a dataframe (df) with ~100 variables, and I would like to do a multiple regression between the response which is my First variable (Y) and the variables 25 to 60 as regressors. The problem is that I don't want to write each variable name like:
lm(Y~var25+var26+.......var60, data=df)
I would like to use something like [, 25:60] to select a complete range. I have tried it but doesn't works:
test <- lm(Y~df[, 25:60], data=df)
summary(test)
some idea?
You could
subset
the dataset by selecting only those columns, and then do thelm
.Suppose, if you need
var25
tovar60
and if the data is ordered by column namesOr another option would be to use
paste
to create the formuladata