10

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!

1
  • Can you try to give an example of what your data is, and the desired output? Commented Mar 7, 2013 at 12:01

3 Answers 3

11

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
Sign up to request clarification or add additional context in comments.

1 Comment

The assign function will not easily assign into a data frame (as the first part of the question mentions). Also see fortune(236).
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) 

2 Comments

I think he wants to assign 1, 2, 3 to monday, tuesday and wednesday respectively..? not sure, if I got it right though.
This is exacttly what the title says, and the OP should be using lists in this case.
3

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).

1 Comment

+1 for fortune(106): If the answer is parse() you should usually rethink the question. —Thomas Lumley - R-help (February 2005)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.