我正在尝试使用 as.name(x) 来引用要输入到函数中的列表。这是我的 stats 函数的简化版本示例,后跟用于一次输出所有数据的 for 循环。
get<-function(data,x) {
for (i in x) {
lm(as.formula(paste(i,'~',variable)),data)
}
}
lists<-c("a","b","c")
# where each of a, b, and c are lists that refer to column names of my data
for (j in lists) {
get(data,as.name(j))
}
我不断收到以下错误:
Error in for (i in x) { : invalid for() loop sequence
如果我每次都只做 get(data,a) ,但当我尝试做一个循环时不做。
最佳答案
都是a
, b
和 c
一个只包含一个值的列表?我问是因为你的lm()
公式有i
在左侧,并且只能是一个向量。
如果是这种情况,则更换 as.name(j)
与 j
应该让你的代码工作。
关于r - for() 循环序列无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45469198/