title: For each column in R data frame
body: <p>I was wondering how for loops work in R data frames. This is not a reproducible example, but I'm wondering if the concept can work. If df has a Date, ID, Amount, and 4 variables, can I loop through the columns? I need to remove NA rows from columns Var1 to Var4, create a "weight vector" based off of the Amount column, then calculate the weighted mean.</p>
  
  <pre><code>a&lt;- names(df)
  a&lt;- a[4:7]
  
  a
  [1] "Var1" "Var2" "Var3" "Var4"
  
  
  #df has Date, ID, Amount ,Var1, Var2, Var3, Var4
  
  for(i in a) {
  
    NEW &lt;-df[ !is.na(df$i), ]
    NEW &lt;- NEW %&gt;%
      group_by(Date) %&gt;%
      mutate(Weights = Amount/sum(Amount))
  
    SUM &lt;-  NEW %&gt;%
      group_by(Date) %&gt;%
      summarise(Value = weighted.mean(i, Weights))
  
    write.csv(SUM , paste0(i, ".csv"))
  
  }
  </code></pre>