简体   繁体   中英

String to variable name in R

I am cleaning a data set and I need to choose the variables depending on another variable. Let's say that if ID = 1 , I need to introduce in the data frame the variable VAR01 , if ID = 2 , I need VAR02 , and so on.

Thus, I'm doing a for loop where I paste the variable name 'VAR' with the ID number with the stringf function. The problem is that I need R to understand the string as a function name.

I've found in the forum this solution, which doesn't work for me:

> variable1 = c("monday", "tuesday", "wednesday")

> var_name = "variable1"

> eval(parse(text=var_name))
[1] "monday"    "tuesday"   "wednesday"

The problem is I cannot use it to refer to the variable:

> eval(parse(text=var_name)) = c(1,2,3)
Error in file(filename, "r") : cannot open the connection
In addition: Warning message:
In file(filename, "r") :
cannot open file 'variable1': No such file or directory

Has anyone got a solution?

Thanks!

You can use assign()

var_name <- 'test'
assign(var_name,1:3)
test

Note: assign as such will create the variable in the environment it is called. So, if you were to call assign within a function like this:

myfun <- function(var) { 
    assign(eval(substitute(var)), 5)
    print(get(var)) 
}

Calling the function assigns my_var a value of 5 within the function, whose environment is created only for the time the function is run and destroyed after.

> myfun("my_var")
# [1] 5

> my_var
# Error: object 'my_var' not found

So, if you want to retain the value after a function call, then, you'll have to specify an environment you'll have the variable thro' the time your task is run. For example, in the global environment :

myfun <- function(var, env = globalenv()) {
    assign(eval(substitute(var)), 5, envir = env)
    print(get(var))
}

> myfun("my_var")
# [1] 5
>  my_var
# [1] 5

You can use get :

result = get(var_name)

Although heavy use of get and set suggests you might want to start using list 's of variables instead:

l = list(variable1 = c(1,2,3))
l$variable1 = c(4,5,6) 

This is FAQ 7.21.

The most important part of that FAQ is the end where it tells you to use lists instead of doing this in the global environment. You mention that you want to do this in a data frame (which is already a list), so this becomes simpler. Try something like:

mydf <- data.frame( g=c('a','b','c') )
ID <- 1

mydf[[ sprintf("VAR%02d",ID) ]] <- 1:3
mydf

Using eval(parse(text=...)) in this case is like saying that you know how to get from New York City to Boston and therefore ask for directions from your origin to New York and directions from Boston to your destination. In some cases this might not be too bad, but it is a bit out of the way if you are trying to get from London to Paris (and your example has you going from New York to Boston via Mars). See fortune(106) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM