首先,我对这个很新,所以我的方法/思路可能是错误的。我已经使用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]], : variable lengths differ (found for 'temp[[i]]')"。如果有人能指导我正确的方向,我将非常感激。谢谢。
res.models[["mpg~disp"]]
,我希望它能够添加所有变量的名称,例如 [[mpg~disp+x2+x3+....x18]。 - Stick