title: Using r for loop to create new columns in data frame
body: <p>I'm trying to create a for loop in r to 1. create new columns in a dataframe and 2. calculate some simple expression over multiple variables.</p>
  
  <p>My dataframe has 10 columns and 22 rows</p>
  
  <pre><code>df &lt;- setNames(data.frame(replicate(22,sample(0:10,10,rep=TRUE))), 
       sprintf("rmeas%s",seq(from = 0, to = 84, by = 4)))
  </code></pre>
  
  <p>I'd like to create a simple for loop to create 21 new columns named 'rmaster' followed by a number (4-84 by 4). The first new column (df$rmaster4) will be populated by the following calculation:</p>
  
  <pre><code>df$rmaster4 &lt;- (df$rmeas4^3 + df$rmeas0*df$rmeas4+ df$rmeas0^2) / 2.12352
  </code></pre>
  
  <p>So far, I have the following code:</p>
  
  <pre><code>for(i in seq(from = 0, to = 84, by = 4)) {
  
  assign("df", `$&lt;-`(df, paste0("rmaster", i+4), 
                (get(paste0("x$rmeas", i+4))^3 + 
                   get(paste0("x$rmeas", i))*get(paste0("x$rmeas", i+4)) + 
                   get(paste0("x$rmeas", i+4))^2) / 2.12352))
  }
  </code></pre>
  
  <p>There seems to be a lot going wrong here. For starters, I want to create rmaster4-rmaster84 and this code would create rmaster4-rmaster88. Next, I know I can't use the get() function in the way that I have. Unfortunately, I am unable to remedy the problems. Any insight would be greatly appreciated.</p>