【发布时间】:2018-03-11 14:32:44
【问题描述】:
首先,我对此很陌生,所以我的方法/想法可能是错误的,我已经使用 R 和 R Studio 将 xlsx 数据集导入到数据框中。我希望能够遍历列名以获取所有带有“10”的变量,以便运行简单的线性回归。所以这是我的代码:
indx <- grepl('_10_', colnames(data)) #list returns all of the true values in the data set
col10 <- names(data[indx]) #this gives me the names of the columns I want
这是我的 for 循环,它返回一个错误:
temp <- c()
for(i in 1:length(col10)){
temp = col10[[i]]
lm.test <- lm(Total_Transactions ~ temp[[i]], data = data)
print(temp) #actually prints out the right column names
i + 1
}
甚至可以运行一个循环将这些变量放入线性回归模型中吗?我得到的错误是:“model.frame.default 中的错误(formula = Total_Transactions ~ temp[[i]],:可变长度不同(为 'temp[[i]]' 找到)”。如果有人能指出我在正确的方向上,我将非常感激。谢谢。
【问题讨论】:
-
看看this的问题。然后,如果您仍然需要帮助,请说出来。
-
@RuiBarradas 我尝试了那个代码,但最后是
res.models[["mpg~disp"]],我希望它附加所有变量名,例如 [[mpg~disp+x2+x3+.... x18]。
标签: r loops linear-regression modeling