bragks
September 28, 2018, 12:08pm
1
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?
mara
September 28, 2018, 12:47pm
2
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.
When I am generating output from an R Markdown document, I often want to output a table of numbers.
The suggestions for formatting tables in the Markdown cheatsheet all seem to be geared at model-output or statistical result-type tables. That's not what I need.
What I am looking for is a "presentation-friendly" table. More of a business application.
Something that makes it easy to define the format of each column (decimal places, commas, % signs, date formats, etc.) and that lets me define the borders (existence, size, colour) and shading of the tables and its cells. And the width of the table.
A typical example would be a table with 4 columns: Year/Quarter, Count, Amount, Percentage Cha…
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
mara
September 28, 2018, 12:52pm
4
Oh yeah, for sure you can reshape your data to contain what you described with tidyr/dplyr!
1 Like
bragks
October 1, 2018, 6:38am
5
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?
mara
October 1, 2018, 9:54am
6
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