title: Calling & creating new columns based on string
body: <p>I have searched quite a bit and not found a question that addresses this issue--but if this has been answered, forgive me, I am still quite green when it comes to coding in general. I have a data frame with a large number of variables that I would like to combine &amp; create new variables from based on names I've put in a 2nd data frame in a loop. The data frame <code>formulas</code> should create &amp; call columns from the main data frame <code>data</code></p>
  
  <pre><code>USDb = c(1,2,3)
  USDc = c(4,5,6)
  EURb = c(7,8,9)
  EURc = c(10,11,12)
  data = data.frame(USDb, USDc, EURb, EURc)
  </code></pre>
  
  <p>Now I'd like to create a new column <code>data$USDa</code> as defined by </p>
  
  <pre><code>data$USDa = data$USDb - data$USDc
  </code></pre>
  
  <p>and so on for EUR and other variables. This is easy enough to do manually, but I'd like to create a loop that pulls the names from <code>formulas</code>, something like this:</p>
  
  <pre><code>a = c("USDa", "EURa")
  b = c("USDb", "EURb")
  c = c("USDc", "EURc")
  formulas = data.frame(a,b,c)
  
  for (i in 1:length(formulas[,a])){
      data$formulas[i,a] = data$formulas[i,b] - data$formulas[i,c]
      }
  </code></pre>
  
  <p>Obviously <code>data$formulas[i,a]</code> this returns <code>NULL</code>, so I tried <code>data$paste0(formulas[i,a])</code> and that returns <code>Error: attempt to apply non-function</code></p>
  
  <p>How can I get these strings to be recognized as variables in this way? Thanks.</p>