Anyone have thoughts on how to get the v1.fac
Green category to appear in the table, despite having 0 obs? I would like all 4 colors to appear. Here’s my latest attempt:
library(survey)
library(gtsummary)
df <- data.frame(v1 = c(1,1,4,4,4,4,2,4,1,1,1,2,2,2,4,2,1,2,1,4),
v2 = c(3,2,1,2,3,2,3,2,3,1,1,3,3,2,1,3,2,2,1,2),
agecat = c(1,3,1,2,3,2,3,2,3,1,2,3,1,2,1,3,1,2,1,NA),
str = c(1,4,1,2,3,6,5,1,4,6,3,4,5,6,1,5,3,2,1,4),
wgts = c(1.28,1.77,1.28,1.40,1.44,1.71,1.04,1.28,1.77,1.71,1.44,1.77,1.04,1.71,1.28,1.04,1.44,1.40,1.28,1.77))
df <- df %>% mutate(v1.fac = factor(v1, levels = c(1,2,3,4), labels = c('Red','Blue','Green','Black')),
v2.fac = factor(v2, levels = c(1,2,3), labels = c('Small','Medium','Large')),
agecat.fac = factor(agecat, levels = c(1,2,3), labels = c('<18','18-54','54+')))
tabl <- svydesign(id = ~1, strata = ~str, weights = ~wgts, data = df) %>%
tbl_strata(strata = c('v1.fac'), .combine_with = c('tbl_merge'), .tbl_fun = ~ .x %>%
tbl_svysummary(include='agecat.fac', by='v2.fac', missing='no', label=agecat.fac~'Age', statistic=agecat.fac~'{n}') %>%
modify_header(all_stat_cols() ~ '{level}') %>%
modify_footnote(all_stat_cols() ~ NA))
tabl
Still no Green category. Yikes!