Publication ready output from summarise_all

library(dplyr)

mtcars %>% 
  select(mpg, hp, cyl) %>% 
  group_by(cyl) %>% 
  summarise_all(funs(mean, sd)) 
#> # A tibble: 3 x 5
#>     cyl mpg_mean hp_mean mpg_sd hp_sd
#>   <dbl>    <dbl>   <dbl>  <dbl> <dbl>
#> 1     4     26.7    82.6   4.51  20.9
#> 2     6     19.7   122.    1.45  24.3
#> 3     8     15.1   209.    2.56  51.0

Created on 2018-09-28 by the reprex package (v0.2.1)

How could I change this output into something more publication friendly? Let's say with cyl as columns, mpg and hp as rows with mean (sd) as values. Should I even be using summarise_all for this purpose?

For the purpose of generating a table for print? Probably not. There are lots of table-making packages, but this one came to mind, given the way in which you described your desired output:
https://cran.r-project.org/web/packages/qwraps2/vignettes/summary-statistics.html
Example from vignette:

There are quite a few threads on options here, too, e.g.

4 Likes

This can be achieved with some data manipulation after your summarize_all call, like this:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

mtcars %>% 
  select(mpg, hp, cyl) %>% 
  group_by(cyl) %>% 
  summarise_all(funs(mean, sd)) %>% 
  gather(key = key, value = value, -cyl) %>% 
  separate(key, into = c("type", "stat"), sep = "_") %>% 
  spread(key = stat, value = value) %>% 
  mutate(mean_w_sd = paste0(round(mean, 2), " (", intToUtf8("177"), round(sd, 2), ")")) %>% 
  select(cyl, type, mean_w_sd) %>% 
  spread(key = cyl, value = mean_w_sd)
#> # A tibble: 2 x 4
#>   type  `4`            `6`             `8`            
#>   <chr> <chr>          <chr>           <chr>          
#> 1 hp    82.64 (±20.93) 122.29 (±24.26) 209.21 (±50.98)
#> 2 mpg   26.66 (±4.51)  19.74 (±1.45)   15.1 (±2.56)

Created on 2018-09-28 by the reprex package (v0.2.0).

Granted, it may not be the most elegant method but it gets what you wanted. You could do some minor things after this, like renaming columns, but this accomplishes what you are after.

1 Like

Oh yeah, for sure you can reshape your data to contain what you described with tidyr/dplyr!

1 Like

Sweeeeeet, qwraps2 looks pretty promising!

This is a slight diversion from the original question, but in general I guess most(all?) of these packages are "ment" for rmarkdown or at least output the tables to html? I'm writing in google docs - should I assume the tables from most packages would work (with some simple formatting) after copy/pasting from html, or is there something I should look out for?

There are packages that are expressly meant for use with docs, so I'd recommend looking at those, if that's your intended output.

e.g. tangram
https://cran.r-project.org/web/packages/tangram/index.html

2 Likes