title: tidyr unnest, prefix column names with nested name during unnesting
body: <p>When running <code>unnest</code> on a <code>data.frame</code> is there a way to add the group name of nested item to the individual columns it contains (either as a suffix or prefix). Or does renaming have to be done manually via <code>rename</code>?</p>
  
  <p>This is particularly relevant with 'unnesting' multiple groups that contain columns with the same names. </p>
  
  <p>In the example below the <code>base</code> <code>aggregate</code> command does this well (eg. Petal.Length.mn), but I couldn't find an option to get <code>unnest</code> to do the same thing? </p>
  
  <p>I'm using <code>nest</code> with <code>purrr::map</code> as I want the flexibility to mix functions, eg. calculate means and sd on a couple of variables and also run a t test to look at differences between them.</p>
  
  <pre class="lang-r prettyprint-override"><code>
  library(dplyr, warn.conflicts = FALSE)
  
  msd_c &lt;- function(x) c(mn = mean(x), sd = sd(x))
  msd_df &lt;- function(x) bind_rows(c(mn = mean(x), sd = sd(x)))
  
  aggregate(cbind(Petal.Length, Petal.Width) ~ Species, 
            data = iris, FUN = msd_c)
  #&gt;      Species Petal.Length.mn Petal.Length.sd Petal.Width.mn Petal.Width.sd
  #&gt; 1     setosa       1.4620000       0.1736640      0.2460000      0.1053856
  #&gt; 2 versicolor       4.2600000       0.4699110      1.3260000      0.1977527
  #&gt; 3  virginica       5.5520000       0.5518947      2.0260000      0.2746501
  
  iris %&gt;% 
    select(Petal.Length:Species) %&gt;% 
    group_by(Species) %&gt;% 
    tidyr::nest() %&gt;% 
    mutate(
      Petal.Length = purrr::map(data, ~ msd_df(.$Petal.Length)),
      Petal.Width = purrr::map(data, ~ msd_df(.$Petal.Width)),
      Correlation = purrr::map(data, ~ broom::tidy(cor.test(.$Petal.Length, .$Petal.Width))),
    ) %&gt;% 
    select(-data) %&gt;% 
    tidyr::unnest(c(Petal.Length, Petal.Width, Correlation), names_repair = tidyr::tidyr_legacy)
  #&gt; # A tibble: 3 x 13
  #&gt; # Groups:   Species [3]
  #&gt;   Species    mn    sd   mn1   sd1 estimate statistic  p.value parameter conf.low
  #&gt;   &lt;fct&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;     &lt;int&gt;    &lt;dbl&gt;
  #&gt; 1 setosa   1.46 0.174 0.246 0.105    0.332      2.44 1.86e- 2        48   0.0587
  #&gt; 2 versic~  4.26 0.470 1.33  0.198    0.787      8.83 1.27e-11        48   0.651 
  #&gt; 3 virgin~  5.55 0.552 2.03  0.275    0.322      2.36 2.25e- 2        48   0.0481
  #&gt; # ... with 3 more variables: conf.high &lt;dbl&gt;, method &lt;chr&gt;, alternative &lt;chr&gt;
  </code></pre>
  
  <p><sup>Created on 2020-05-20 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>