首先,我对此非常陌生,所以我的方法/想法可能是错误的,我使用R和R将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中的错误(公式= Total_Transactions ~ temp[i],:变量长度不同(在”temp[i]“中找到)”。如果有人能给我指明正确的方向,我将非常感激。谢谢。
发布于 2017-09-29 16:44:31
您可以创建一个临时子集,其中只选择在回归中使用的列。这样,您就不需要在公式中插入临时名称。
坚持你的代码,这应该是可行的。
for(i in 1:length(col10)){
tempSubset <- data[,c("Total_Transactions", col10[i]]
lm.test <- lm(Total_Transactions ~ ., data = tempSubset)
i + 1
}https://stackoverflow.com/questions/46493011
复制相似问题