title: Using mutate_at() on a nested dataframe column to generate multiple unnested columns
body: <p>I'm experimenting with <code>dplyr</code>, <code>tidyr</code> and <code>purrr</code>. I have data like this:</p>
  
  <pre><code>library(tidyverse)
  
  set.seed(123)
  df &lt;- data_frame(X1 = rep(LETTERS[1:4], 6),
                   X2 = sort(rep(1:6, 4)),
                   ref = sample(1:50, 24),
                   sampl1 = sample(1:50, 24),
                   var2 = sample(1:50, 24),
                   meas3 = sample(1:50, 24))
  </code></pre>
  
  <p>Now <code>dplyr</code> is awesome because I can do things like <code>mutate_at()</code> to manipulate multiple columns at once. e.g:</p>
  
  <pre><code>df &lt;- df %&gt;% 
    mutate_at(vars(-one_of(c("X1", "X2", "ref"))), funs(first = . - ref)) %&gt;% 
    mutate_at(vars(contains("first")),  funs(second = . *2 ))
  </code></pre>
  
  <p>and <code>tidyr</code> allows me nest subsets of the data as sub-tables in a single column:</p>
  
  <pre><code>df &lt;- df %&gt;% nest(-X1) 
  </code></pre>
  
  <p>and thanks to <code>purrr</code> I can summarize these sub-tables while retaining the original data in the nested column:</p>
  
  <pre><code>df %&gt;% mutate(mean = map_dbl(data, ~ mean(.x$meas3_first_second)))
  </code></pre>
  
  <p>How can I use <code>purrr</code> and <code>mutate_at()</code> to generate multiple summary columns (take the means of different (but not all) columns in each nested sub-table)? </p>
  
  <p>In this example I'd like to take the mean of every column with the word "second" in it.I had hoped that this might produce a new nested column which I could then <code>unnest()</code> but it does not work.</p>
  
  <pre><code>df %&gt;% mutate(mean = map(data, ~ mutate_at(vars(contains("second")),
                                             funs(mean_comp_exp = mean(.)))))
  </code></pre>
  
  <p>How can I achieve this?</p>