I have table with variable names and observation for this variable. Some of these observations should be saved to variable as text.
I can't find any other way to deal with except the following, which is not working:
x <- data.frame( c("Name1","Name2","Name3"),
c("Result1",2,"Result3") )
txt <- paste(x[,1], "<-", x[,2], sep="")
eval(parse(text=txt))
Error in eval(parse(text = txt)) : object 'Result1' not found
I need to get the result where Name1 = "Result1"
Ideally you should not be creating such multiple objects in the global environment. It is difficult to manage them. However, for some reason if you have to do it here is one way :
data <- type.convert(setNames(as.list(x[[2]]), x[[1]]), as.is = TRUE)
list2env(data, .GlobalEnv)
We create a named list where value is from 2nd column and name is from 1st column of x
. We then use type.convert
to convert the data into their respective types. Finally use list2env
to have Name1
, Name2
, Name3
as variables in your global environment.