title: Variable assignment within a for-loop
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6034655/r-how-to-convert-string-to-variable-name">R: How to convert string to variable name?</a>  </p>
  </blockquote>
  
  
  
  <p>In R, I'm writing a for-loop that will iteratively create variable names and then assign values to each variable.</p>
  
  <p>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.  </p>
  
  <p>(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.)</p>
  
  <pre><code>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)
  }
  </code></pre>
  
  <p>Now, varName prints out as</p>
  
  <pre><code> [1] NA NA NA NA NA NA NA NA NA NA NA NA
  </code></pre>
  
  <p>and Variable1 is not found.</p>
  
  <p>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.  </p>
  
  <p>I think the more general issue is assignment vs. equality. In the first line, I want varName to <em>be equal to</em> the newly made string, but in the next line, I want varName to <em>be assigned to</em> the NA value vector.</p>
  
  <p>What is the simplest way of creating that distinction? I'm also open to entirely different, better ways to go about this.</p>
  
  <p><em>EDIT: Changed title because I had mischaracterized the problem.</em></p>