title: R lapply looping into flexible vector and rename variable through suffix
body: <p>I have a flexible vector of combinations (in real life it could vary a lot and depends on an external table, so I could not slicing or using across, or something depending on the name itself of variables in my df).</p>
  <p>I would like to group/sum up the variables in my df, whose name matches the names in &quot;possible comb&quot; vector. Then applying a &quot;_sum&quot; suffix to output variable names, e.g. Jon.A_sum.</p>
  <p>In my df, I have several variables, not all should be summed up, but only a selected and flexable list matching with &quot;possible comb&quot; names.</p>
  <p>In this code I miss how to rename the output variables with _sum suffix in the lapply step, if possible, but I'm open to other approaches of looping.</p>
  <pre><code>
  possible_comb &lt;- c(&quot;Jon.A&quot;, &quot;Bill.C&quot;, &quot;Maria.E&quot;, &quot;Ben.D&quot;)
  
  Jon.A &lt;- c(23, 41, 32, 58, 26)
  Bill.C &lt;- c(13, 41, 35, 18, 66)
  v3 &lt;- c(3,34, 33, 34, 23)
  weight &lt;- c(2, 2, 3,3, 6)
  
  df &lt;- data.frame(Jon.A,Bill.C,v3,weight)
  
  setDT(df)
  
  df_grouped&lt;- df[, lapply(.SD, sum), by=c(&quot;weight&quot;) , .SDcols=possible_comb] 
  
  #wanted results
  
  Jon.A_sum &lt;- c(64, 90, 26)
  Bill.C_sum &lt;- c(54,53, 66)
  weight &lt;- c(2,3, 6)
  
  wanted &lt;- data.frame(Jon.A_sum,Bill.C_sum,weight)
  
  </code></pre>