perondi
perondi

Reputation: 165

string to variable in R

I'm working on an algorithm that R contains 22 raster cells. These are being stored in memory. Each variable name that gun vector.

var_names <- c(list1,list2,list3,list4)

I use the following function, but it makes me only the first member in the vector variable.

eval(parse(text=var_names))

I need all the elements of the vector become variables.

Upvotes: 0

Views: 80

Answers (1)

sckott
sckott

Reputation: 5893

Is this what you had in mind?

list1 <- list2 <- list3 <- list4 <- list(1,2)
var_names <- c("list1","list2","list3","list4")
sapply(var_names, function(x) eval(parse(text=x)))

     list1 list2 list3 list4
[1,] 1     1     1     1    
[2,] 2     2     2     2    

Upvotes: 1

Related Questions