title: Generate grouped std deviation columns with summarise() and t.test() results
body: <p>I am performing many t-tests with similar grouping variables called <code>group</code> (0 or 1) to test the difference in means, for 2* <code>instruments</code>. The groups are defined differently according to <code>external</code>. To streamline output, I am using <code>dplyr()</code> to perform t-test multiple times and then <code>tidy</code> to clean up the t-test outputs.</p>
  <p>QUESTION: How can I add 2 columns: sd1 with the standard deviation for estimate1 and sd2for std dv of estimate 2. That way I can report mean (SD) for each group in the t-test, for each external measure and for each instrument. So each row within the columns should have a unique SD</p>
  <p>*in reality, many more</p>
  <pre><code>library(tidyverse)
  library(dplyr)
  library(broom)
  
  # Example dataframe
  data &lt;- structure(list(external = c(&quot;ex1&quot;, &quot;ex2&quot;, &quot;ex1&quot;,  &quot;ex1&quot;, &quot;ex3&quot;, &quot;ex1&quot;, &quot;ex1&quot;, &quot;ex1&quot;,  &quot;ex2&quot;, &quot;ex3&quot;, &quot;ex2&quot;, &quot;ex1&quot;, &quot;ex2&quot;, &quot;ex3&quot;, &quot;ex3&quot;, &quot;ex2&quot;, &quot;ex1&quot;, &quot;ex3&quot;, &quot;ex2&quot;, &quot;ex1&quot;, &quot;ex1&quot;, &quot;ex2&quot;, &quot;ex3&quot;, &quot;ex1&quot;, &quot;ex3&quot;, &quot;ex1&quot;, &quot;ex2&quot;, &quot;ex2&quot;, &quot;ex2&quot;, &quot;ex1&quot;
  ), group = structure(c(2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 
                         1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 
                         2L, 2L, 1L, 1L), .Label = c(&quot;1&quot;, &quot;0&quot;), class = &quot;factor&quot;), instrument = c(&quot;in1&quot;, &quot;in2&quot;, &quot;in1&quot;, &quot;in2&quot;, &quot;in2&quot;,                                                                    &quot;in2&quot;, &quot;in1&quot;, &quot;in1&quot;, &quot;in2&quot;,                                                                    &quot;in2&quot;, &quot;in1&quot;, &quot;in2&quot;, &quot;in2&quot;,                                                                    &quot;in2&quot;, &quot;in2&quot;, &quot;in1&quot;, &quot;in1&quot;,                                                                    &quot;in1&quot;, &quot;in1&quot;, &quot;in2&quot;, &quot;in1&quot;,                                                                    &quot;in2&quot;, &quot;in1&quot;, &quot;in1&quot;, &quot;in1&quot;,                                                                   &quot;in2&quot;, &quot;in2&quot;, &quot;in1&quot;, &quot;in1&quot;, 
                                                                                                  &quot;in2&quot;), changescore = c(0.044, 0.228, 0.39840119328, 0.055422675, 0.292, 0.388, 0.741, 0.037, 0.182, 0.0459999999999999, 0.05105604, 0, 0, 0, 0.646, 0.15, 0.0379999999999999, -0.102,   0.37, 0.396, 0.109, 0, 0.633, 0.247, 0.151, 0.0449999999999999,  0.172, 0, 0, 0.223473)), row.names = c(NA, -30L), class = &quot;data.frame&quot;)
    
   
  # Perform t-tests on 2 groups for each external &amp; instrument
  data %&gt;%
    group_by(external, instrument) %&gt;%              
    summarise(tidy(t.test(changescore ~ group,     
                          var.equal = T  
                          )))
  
  
  
  ## PRODUCES:
  
  # A tibble: 6 x 12
  # Groups:   external [3]
    external instrument estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method        alternative
    &lt;chr&gt;    &lt;chr&gt;         &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;chr&gt;         &lt;chr&gt;      
  1 ex1      in1          0.0132     0.234    0.221     0.0549 0.958           5   -0.605     0.631 Two Sample t… two.sided  
  2 ex1      in2          0.302      0.336    0.0335    5.15   0.00676         4    0.139     0.465 Two Sample t… two.sided  
  3 ex2      in1          0.0654     0.140    0.075     0.410  0.710           3   -0.442     0.573 Two Sample t… two.sided  
  4 ex2      in2          0.140      0.228    0.0885    1.22   0.310           3   -0.224     0.503 Two Sample t… two.sided  
  5 ex3      in1          0.608      0.633    0.0245    2.78   0.220           1   -2.18      3.39  Two Sample t… two.sided  
  6 ex3      in2          0.328      0.328    0         0.942  0.446           2   -1.17      1.83  Two Sample t… two.sided  
  
  </code></pre>
  <p>*Note: <code>estimate</code> = difference in means, <code>estimate1</code> = mean for group 1, <code>estimate2</code> = mean group 2.</p>
  <p>QUESTION: How can I add 2 columns: <code>sd1</code> with the standard deviation for estimate1 and <code>sd2</code>for std dv of estimate 2. That way I can report mean (SD) for each group in the t-test, for each <code>external</code> measure and for each <code>instrument</code>. So each row within the columns should have a unique SD</p>
  <p>I have tried adding snippets like <code>summarise (sd1   = sd( changescore, na.rm = T) )</code> within the <code>summarise()</code> function, but it either produces NAs down the whole column, or repeats the same value. Adding <code>mutate()</code> after did not produce as desired. Not sure where or what to add if the data are already grouped &amp; summarized...</p>
  <p>Thanks!</p>