title: Apply calculation to specified number of columns and store the result in separate objects, to be combined at the end
body: <p>I have a data frame in which I want to apply a calculation to a varying amount of columns that are specified, and store the results in separate objects, to be combined at the end.</p>
  <p>A minimal example would look like:</p>
  <pre><code>Name &lt;- c(&quot;Case 1&quot;, &quot;Case 2&quot;, &quot;Case 3&quot;, &quot;Case 4&quot;, &quot;Case 5&quot;)
  Base &lt;- c(0, 0, 0, 1, 1)
  C1 &lt;- c(1, 0, 1, 1, 0)
  C2 &lt;- c(0, 1, 1, 1, 0)
  C3 &lt;- c(0, 1, 0, 0, 0)
  C4 &lt;- c(1, 1, 0, 1, 0)
  Data &lt;- data.frame(Name, Base, C1, C2, C3, C4)
  
  score.calc &lt;- function(data, col.names){
  
                         # This is how I would to it outside a function and without loop:
                         Score1 &lt;- sum(pmin(Data$C1, pmin(Data$Base)))/sum(pmin(Data$Base))
                         Score2 &lt;- sum(pmin(Data$C2, pmin(Data$Base)))/sum(pmin(Data$Base))
                         Score3 &lt;- sum(pmin(Data$C3, pmin(Data$Base)))/sum(pmin(Data$Base))
                         Scores &lt;- c(Score1, Score2, Score3)
  }
  
  new.score &lt;- score.calc(Data,
                          col.names= c(&quot;C1&quot;, &quot;C2&quot;, &quot;C3&quot;))
  </code></pre>
  <p>And should return:</p>
  <pre><code>&gt; new.score
  [1] 0.5 0.5 0.0
  </code></pre>
  <p>Anyone with an idea? Many thanks!</p>