title: How to loop over column names that contain a variable in R?
body: <p>I have a dataframe df:</p>
  
  <pre><code>Shares    Price1    Price2    Price3
  100       9         10        11
  200       5         6         7
  300       3         2         1
  </code></pre>
  
  <p>And I would like to loop over this dataframe and create three new columns equal to Shares x Price[i] where (i in 1:3). I tried the following code:</p>
  
  <pre><code>for (j in 1:3) {
    df$paste0("MktCap",j,sep="")&lt;-df$Shares*df$paste0("Price",j,sep="")
  }
  </code></pre>
  
  <p>But I get the error:</p>
  
  <pre><code>Error: attempt to apply non-function
  </code></pre>
  
  <p>I've looked <a href="https://stackoverflow.com/questions/29732858/r-define-column-name-with-paste">here</a> but it's not quite what I want as I would like my new column names to iterate.</p>