gpt4 book ai didi

R: eval(parse()) 错误消息:即使在解析中指定了 "text=",也无法打开文件

转载 作者:行者123 更新时间:2023-12-01 12:54:25 36 4
gpt4 key购买 nike

我多次对国家列表进行分析,在每次迭代期间,结果应添加到向量中。下面我展示了一个没有循环的简化示例,仅适用于一个国家。即使我彻底寻找解决方案,我也找不到答案。

#this is my simplified country vector with just 1 country    
country<-c("Spain")

#This is the vector that should hold the results of multiple iterations
#for now, it contains only the result of the first iteration
Spain.condition1<- 10

#reading result vector in a variable (this is automized in a loop in my code)
resultVector<-paste(country,"condition1",sep=".")

#when I call the content of the vector with parse, eval
#I see the content of the vector as expected

eval(parse(text=resultVector))

#however, when I try to add a second result to it

eval(parse(text=resultVector))[2]<-2

#I get following error message:

#Error in file(filename, "r") : cannot open the connection
#In addition: Warning message:
#In file(filename, "r") :
# cannot open file 'Spain.condition1': No such file or directory

谁能帮助我或让我朝着正确的方向前进?

最佳答案

分配给 eval不能保证工作。这是使用 eval 通常不是一个好主意的多种原因之一。 .

为什么不将国家及其条件存储在命名列表中,如下所示:

conditions = list()
conditions[["Spain"]] = list()
conditions[["Spain"]][["condition1"]] <- 10
conditions[["Spain"]][["condition1"]][2] <- 2

conditions[["Spain"]][["condition1"]]
# [1] 10 2

ETA:使用循环(我不确切知道您的问题的结构是什么,但总体思路是这样):
countries = c("Spain", "England", "France", "Germany", "USA") # and so on
conditions = c("Sunny", "Rainy", "Snowing") # or something

data = list()
for (country in countries) {
data[[country]] <- list()
for (condition in conditions) {
data[[country]][[condition]] <- 4 # assign appropriate value here
}
}

它也可以从一个制表符分隔的文件中构建,或者以适合您的问题的任何方式生成 - R 的功能非常强大。

关于R: eval(parse()) 错误消息:即使在解析中指定了 "text=",也无法打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10611209/

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