R: Multiple Linear Regression with a specific range of variables [duplicate]

放肆的年华 提交于 2019-12-02 01:58:20

You could subset the dataset by selecting only those columns, and then do the lm.

lm(Y~., data=df1[c(1,25:60)])

Suppose, if you need var25 to var60 and if the data is ordered by column names

lm(Y~., data=df1[c(1,26:61)])   

Or another option would be to use paste to create the formula

lm(paste("Y ~", paste(paste0('var', 25:60), collapse="+")), data=df1)

data

set.seed(24)
df1 <- as.data.frame(matrix(sample(1:80, 20*101, replace=TRUE),
   ncol=101, dimnames=list(NULL, c('Y', paste0('var', 1:100)))))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!