r - Combining multiple summary tables with subheadings -
i have data several demographic factors.
i trying create publication-quality summary table this:
n sex m 150 f 150 marital status single 100 married 100 divorced 100 age <25 75 25-34 75 35-44 75 >= 45 75
i can generate each individual piece of this, so:
require(dplyr) dd <- data.frame(barcode = c("16929", "64605", "03086", "29356", "23871"), sex = factor(c("m", "f", "m", "f", "m")), marital = factor(c("married", "single", "single", "single", "divorced")), age_group = factor(c("<25", "25-34", "35-44", "45-54", ">= 55"))) require(dplyr) age_groups <- dd %>% group_by(age_group) %>% count() sex <- dd %>% group_by(sex) %>% count() marital <- dd %>% group_by(marital) %>% count()
and can create individual rmarkdown tables each of them of several solutions, kable
or pander
.
require(knitr) kable(age_groups) kable(sex) kable(marital)
but can't find way combine them parts of single table subheading each category. separate tables have different column widths , aligning them hand , inserting intervening subheading rows (in raw latex?) seems bad solution.
this common reporting format-- standard table 1 many journal articles-- , find general solution creating it.
library(expss) library(knitr) dd = data.frame(barcode = c("16929", "64605", "03086", "29356", "23871"), sex = factor(c("m", "f", "m", "f", "m")), marital = factor(c("married", "single", "single", "single", "divorced")), age_group = factor(c("<25", "25-34", "35-44", "45-54", ">= 55"), levels = c("<25", "25-34", "35-44", "45-54", ">= 55"))) dd %>% tab_cells("sex" = sex, "marital status" = marital, "age" = age_group) %>% tab_cols(total(label = "n")) %>% tab_stat_cases(total_row_position = "none") %>% tab_pivot()
above code produces output in example work html output. second code snippet works knitr formats output differs example.
dd %>% tab_cells("sex" = sex, "marital status" = marital, "age" = age_group) %>% tab_cols(total(label = "n")) %>% tab_stat_cases(total_row_position = "none") %>% tab_pivot() %>% split_columns() %>% kable()
first output:
second output:
Comments
Post a Comment