gpt4 book ai didi

r - for() 循环序列无效

转载 作者:行者123 更新时间:2023-12-04 09:24:38 26 4
gpt4 key购买 nike

我正在尝试使用 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 , bc一个只包含一个值的列表?我问是因为你的lm()公式有i在左侧,并且只能是一个向量。

如果是这种情况,则更换 as.name(j)j应该让你的代码工作。

关于r - for() 循环序列无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45469198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com