To get exactly the format you want, you can use tidyr/dplyr
. With a lot of reshaping, tidying and rearranging...
summary_table = mtcars %>%
group_by(vs) %>%
summarize_all(funs(mean = mean, sd = sd)) %>%
gather("stat", "val", -vs) %>%
mutate(vs = paste0("vs", vs)) %>%
unite(stat, stat, vs, sep = ".") %>%
separate(stat, into = c("var", "stat"), sep = "_") %>%
spread(stat, val) %>%
select(var, mean.vs0, sd.vs0, mean.vs1, sd.vs1) %>%
mutate_if(is.numeric, funs(round(., 3)))
Result with tidyr/dplyr
:
# A tibble: 10 x 5
var mean.vs0 sd.vs0 mean.vs1 sd.vs1
<chr> <dbl> <dbl> <dbl> <dbl>
1 am 0.333 0.485 0.500 0.519
2 carb 3.611 1.539 1.786 1.051
3 cyl 7.444 1.149 4.571 0.938
4 disp 307.150 106.765 132.457 56.893
5 drat 3.392 0.474 3.859 0.506
6 gear 3.556 0.856 3.857 0.535
7 hp 189.722 60.282 91.357 24.424
8 mpg 16.617 3.861 24.557 5.379
9 qsec 16.694 1.092 19.334 1.354
10 wt 3.689 0.904 2.611 0.715
You can also use stargazer
, but I don't think you can combine them:
library(stargazer)
library(dplyr)
mtcars %>%
split(mtcars$vs) %>%
stargazer(type = "text",
summary.stat = c("mean", "sd"),
title = c("vs = 0", "vs = 1"))
Result with stargazer
:
vs = 0
==========================
Statistic Mean St. Dev.
--------------------------
mpg 16.617 3.861
cyl 7.444 1.149
disp 307.150 106.765
hp 189.722 60.282
drat 3.392 0.474
wt 3.689 0.904
qsec 16.694 1.092
vs 0.000 0.000
am 0.333 0.485
gear 3.556 0.856
carb 3.611 1.539
--------------------------
vs = 1
==========================
Statistic Mean St. Dev.
--------------------------
mpg 24.557 5.379
cyl 4.571 0.938
disp 132.457 56.893
hp 91.357 24.424
drat 3.859 0.506
wt 2.611 0.715
qsec 19.334 1.354
vs 1.000 0.000
am 0.500 0.519
gear 3.857 0.535
carb 1.786 1.051
--------------------------
Notes:
- The advantage of the
tidyr/dplyr
method is that the output is a dataframe, so you can manipulate it and use it for further calculations. You can't do that with stargazer
.
- The advantage of the
stargazer
method is that it can output the table in a nice looking table format. Even in Latex. Just change type = "text"
to type = "latex"
. This is especially useful if you want to include descriptive statistics in publication or in the pdf output of your rmarkdown document.
Of course you can also combine the two methods and utilize both benefits:
Result with tidyr/dplyr
+ stargazer
:
> stargazer(summary_table, type = "text", summary = FALSE)
========================================
var mean.vs0 sd.vs0 mean.vs1 sd.vs1
----------------------------------------
1 am 0.333 0.485 0.5 0.519
2 carb 3.611 1.539 1.786 1.051
3 cyl 7.444 1.149 4.571 0.938
4 disp 307.15 106.765 132.457 56.893
5 drat 3.392 0.474 3.859 0.506
6 gear 3.556 0.856 3.857 0.535
7 hp 189.722 60.282 91.357 24.424
8 mpg 16.617 3.861 24.557 5.379
9 qsec 16.694 1.092 19.334 1.354
10 wt 3.689 0.904 2.611 0.715
----------------------------------------
> stargazer(summary_table, type = "latex", summary = FALSE, header = FALSE)
