2

Possible Duplicate:
R: How to convert string to variable name?

In R, I'm writing a for-loop that will iteratively create variable names and then assign values to each variable.

Here is a simplified version. The intention is to create the variable's name based on the value of iterating variable i, then fill the new variable with NA values.

(I'm only iterating 1:1 below since the problem occurs isn't related to the looping itself, but rather to the way the variable is being created and assigned.)

for (i in 1:1) {

    #name variable i "Variablei"
    varName = paste("Variable", as.character(i), sep="")

    #fill variable with NA values
    varName = rep(NA, 12)

    print(varName)
    print(Variable1)
}

Now, varName prints out as

 [1] NA NA NA NA NA NA NA NA NA NA NA NA

and Variable1 is not found.

I understand on some level why this is buggy. In the first line, varName becomes a vector whose only entry is the string "Variable1". Then varName gets reassigned to hold the NA values. So when I try to print Variable1, it doesn't exist.

I think the more general issue is assignment vs. equality. In the first line, I want varName to be equal to the newly made string, but in the next line, I want varName to be assigned to the NA value vector.

What is the simplest way of creating that distinction? I'm also open to entirely different, better ways to go about this.

EDIT: Changed title because I had mischaracterized the problem.

Community
  • 1
  • 1
half-pass
  • 1,851
  • 4
  • 22
  • 33
  • 5
    You might be better off using a list to hold the values. Variable <- list(); Variable[[i]] <- rep(NA,12) – Matthew Lundberg Nov 03 '12 at 21:53
  • 2
    To second @MatthewLundberg's above comment: `lapply(1:1, function(x) rep(NA, 12))` – daroczig Nov 03 '12 at 22:24
  • This approach seems promising. But the reason I was iterating is because I want to name a large number of variables based on incrementally increasing dates. It seems like I'll still need to go through that for-loop to name the list elements, no? – half-pass Nov 03 '12 at 23:17
  • No, the reason others are advocating a single structure (a list) is because you can assign attributes, like names, in one line. See `?names`. – joran Nov 03 '12 at 23:44
  • No need to assign names in one line. No need to add a 'for' loop, and that's because you've already got one. for (...) { name <- something ; myvariablelist[[name]] <- somevalue }. (If you can use 'apply' or one of its friends instead of 'for', you should.) – Matthew Lundberg Nov 04 '12 at 03:27

1 Answers1

13

Assignment works like this:

<varname> = <expression>

or more traditionally

<varname> <- <expression>

So, in your code, you have only ever assigned to varName. It's not about assignment vs equality, just assignment. You may want to look at assign:

for (i in 1:5) {
  assign(paste0("Variable", i), 10*i)
} 

as a toy example.

Moreover, as noted in the comments, there are probably better approaches for your application. For example, why not just use a vector myvector and instead of having variables called Variable1, Variable2, etc you can refer to myvector[1], myvector[2] etc.

As an example, let us say you had planned to work with

Variable1 <- 'foo'
Variable2 <- 'bar'
Variable3 <- 'baz'

then, you could change you approach, and set

mydata <- c('foo', 'bar', 'baz')

and where you would previously have used Variable2 (which contains 'bar') you instead use mydata[2] (which also contains 'bar'). The point here is that it is much easier to work with vectors and dataframes in R than a long list of variables.

You could go further and name the entries:

names(mydata) <- paste0("V", 1:3)

which then allows you to write mydata["V2"] to retrieve bar.

seancarmody
  • 6,182
  • 2
  • 34
  • 31