我多次对国家/地区列表进行分析,在每次迭代期间,结果应添加到向量中。下面我展示了一个简化的示例,仅针对一个国家/地区,没有循环。尽管我彻底寻找解决方案,但我找不到答案。
#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 的能力非常强大。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)