4

I'm running analysis for a list of countries several times and during each iteration the result should be added to a vector. Below I show a simplified example without the loop for just one country. Even though I thoroughly looked for solutions, I could not find an answer.

#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

Could anyone help me or put me in the right direction?

mindless.panda
  • 4,014
  • 4
  • 35
  • 57
user1397524
  • 53
  • 1
  • 5

2 Answers2

2

Assigning to eval isn't guaranteed to work. This is one of multiple reasons it's usually not a good idea to use eval.

Why not just store countries and their conditions in a named list, something like this:

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

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

ETA: To work with a loop (I don't know precisely what the structure of your problem is, but here's the general idea):

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
    }
}

It can also be constructed from a tab-delimited file, or generated in whatever way is appropriate for your problem- R is more than capable.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Thanks! This is a nice solution, however, I have 16 countries and at least 3 conditions, so I would have to add at least 48 lines of code to incorporate your solution. I'm transitioning from SAS to R and in SAS this could be done very elegantly with a macro variable loop. – user1397524 May 16 '12 at 02:54
  • Oh, this can be done *very* easily with a loop. I'm adding an explanation to my answer. – David Robinson May 16 '12 at 02:57
2

David's solution is a lot better but you could do this using get and assign.

country <- "Spain"
Spain.condition1 <- 10
resultVector <- paste(country, "condition1", sep=".")
eval(parse(text=resultVector))
#[1] 10

# Now this is one way to modify that object
# Note that we *need* to assign to a temporary object
# and just using get(resultVector)[2] <- 2 won't work
tmp <- get(resultVector)
tmp[2] <- 2
assign(resultVector, tmp)
Spain.condition1
#[1] 10  2

# We could alternatively do this with eval
# Even if it is a bad idea
eval(parse(text = paste0(resultVector, "[2] <- 3")))
Spain.condition1
#[1] 10  3
Dason
  • 60,663
  • 9
  • 131
  • 148