title: Using 'mutate_' to sum a bunch of columns row-wise
body: <p><a href="http://www.numbertheory.nl/2015/09/23/using-mutate-from-dplyr-inside-a-function-getting-around-non-standard-evaluation/" rel="nofollow noreferrer">In this blog post</a>, Paul Hiemstra shows how to sum up two columns using <code>dplyr::mutate_</code>. Copy/paste-ing relevant parts:</p>
  
  <pre><code>library(lazyeval)
  f = function(col1, col2, new_col_name) {
      mutate_call = lazyeval::interp(~ a + b, a = as.name(col1), b = as.name(col2))
      mtcars %&gt;% mutate_(.dots = setNames(list(mutate_call), new_col_name))
  }
  </code></pre>
  
  <p>allows one to then do:</p>
  
  <pre><code>head(f('wt', 'mpg', 'hahaaa'))
  </code></pre>
  
  <p>Great! </p>
  
  <p>I followed up with a question (see comments) as to how one could extend this to a 100 columns, since it wasn't quite clear (to me) how one could do it without having to type all the names using the above method. Paul was kind enough to indulge me and provided this answer (thanks!):</p>
  
  <pre><code># data
  df = data.frame(matrix(1:100, 10, 10))
  names(df) = LETTERS[1:10]
  
  # answer
  sum_all_rows = function(list_of_cols) {
    summarise_calls = sapply(list_of_cols, function(col) {
      lazyeval::interp(~col_name, col_name = as.name(col))
    })
    df %&gt;% select_(.dots = summarise_calls) %&gt;% mutate(ans1 = rowSums(.))
  }
  sum_all_rows(LETTERS[sample(1:10, 5)])
  </code></pre>
  
  <p>I'd like to improve this answer on these points:</p>
  
  <ol>
  <li><p>The other columns are gone. I'd like to keep them.</p></li>
  <li><p>It uses <code>rowSums()</code> which has to coerce the <em>data.frame</em> to a <em>matrix</em> which I'd like to avoid. </p>
  
  <p>Also I'm not sure if the use of <code>.</code> within <em>non-<code>do()</code></em> verbs is encouraged? Because <code>.</code> within <code>mutate()</code> doesn't seem to adapt to just those rows when used with <code>group_by()</code>.</p></li>
  <li><p>And most importantly, how can I do the same using <code>mutate_()</code> instead of <code>mutate()</code>?</p></li>
  </ol>
  
  <p>I found <a href="https://stackoverflow.com/questions/28159143/add-column-with-row-wise-mean-over-selected-columns-using-dplyr">this answer</a>, which addresses point 1, but unfortunately, both <code>dplyr</code> answers use <code>rowSums()</code> along with <code>mutate()</code>.</p>
  
  <hr>
  
  <p>PS: I just read <a href="https://stackoverflow.com/questions/28159143/add-column-with-row-wise-mean-over-selected-columns-using-dplyr#comment44694397_28159143">Hadley's comment under that answer</a>. IIUC, 'reshape to long form + group by + sum + reshape to wide form' is the recommend <code>dplyr</code> way for these type of operations?</p>