title: Creating new columns using a for loop
body: <p>I am new to R (Economist with background in Stata) and I am having trouble getting a nested for loop to work for me. I know the issue is that I don't have a good understanding of how to use the loop counter as part of a variable name.</p>
  
  <p>A bit of background. I have data frame with data on average rental rates for homes of different size (1 bedroom, 2 bedroom, etc) and data on annual earnings (mean, median, and various percentiles). I am trying to generate a series of new columns containing the ratio of these two things (rental rate / mean earnings).</p>
  
  <p>Specifically my variables are:</p>
  
  <ul>
  <li><p><code>beds1, beds2, beds3, beds4</code></p></li>
  <li><p><code>mean, median, p10, p25, p75, p90</code></p></li>
  </ul>
  
  <p>So you see I need to generate 24 new columns of cost/earnings data. I could write out 24 lines of code but I don't want to. More importantly, I want to learn an efficient way of doing this in R. In Stata I could do this very simply using a nested for loop, but I can't get it to work in R. Here is my code so far.</p>
  
  <pre><code>for (i in 1:4) {
      stat &lt;- c("median", "mean", "p10", "p25", "p75","p90")
      for (x in stat) {
          df$beds[i]_[x] &lt;- round((df$beds[i]/df$[x]),digits=3)
      }
  }
  </code></pre>
  
  <p>When I run this code the error I get is</p>
  
  <pre><code>Error: unexpected input in:
  "    for (x in stat) {
      df$beds[i]_"
  &gt;     }
  Error: unexpected '}' in "    }"
  &gt; }
  Error: unexpected '}' in "}"
  </code></pre>
  
  <p>I have tried to use the double brackets <code>[[]]</code> but that didn't change the results. If anyone has some insight into why the dynamic variables names aren't working please let me know. Even better, since I guess loops are evil in R, if anyone knows a way to use <code>lapply</code> to get this done, I would love to hear that too.</p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>Thanks @Spacedman for the comment. I think I am getting what you're saying. So does that mean that there simply isn't anyway to do what I want to do in R? </p>
  
  <pre><code>var1 &lt;- c("beds1", "beds2")
  var2 &lt;- c("mean", "median")
  
  for (i in 1:2) {
      for (j in 1:2) {
          df$var1[i]_var2[j] &lt;- df$var1[i]/df$var2[j]
      }
  }
  </code></pre>
  
  <p>I think this should grab the elements of the lists <code>var1</code> and <code>var2</code> so that  when <code>i=1</code> and <code>j=1</code>, <code>df$var1[i]/df$var2[j]</code> should mean <code>df$beds1/df$mean</code>. Or would R get mad and think I was trying to divide strings?</p>
  
  <hr>
  
  <p>FINAL EDIT WITH ANSWER FROM @SPACEEMAN</p>
  
  <p>Thanks @Spacedman. I loved your spoiler and thank you for providing additional help. I didn't fully grasp the difference between the two ways of referring to columns after your last post, but I think I have a better idea now. I did a bit of tweaking and now I have something that works perfectly. Thanks again!</p>
  
  <pre><code>beds &lt;- c("beds1", "beds2", "beds3", "beds4")
  stat &lt;- c("median", "mean", "p10", "p25", "p75","p90")
  
  for(i in beds){
      for(x in stat){
          res = paste0(i,"_",x)
          df[[res]]=round(df[[i]]/df[[x]],digits=3)
      }
  }
  </code></pre>