有一个特殊的功能subtotal
:
library(expss)
set.seed(123)
N = 100
X4607 = data.frame(
spcode = sample(c("South", "North"), size = N, replace = TRUE),
qcs1a_SQ001 = sample(c(1:10, 99), size = N, replace = TRUE)
)
X4607 %>%
tab_cells(subtotal(qcs1a_SQ001, "Bottom 3 Box" = 1:3, "Top 3 Box" = 7:10, position = "bottom")) %>%
tab_cols(total(), spcode) %>%
tab_stat_cpct() %>%
tab_last_sig_cpct() %>%
tab_pivot()
# | | #Total | spcode | |
# | | | North | South |
# | | | A | B |
# | ------------ | ------ | ------ | ------ |
# | 1 | 5.0 | 9.3 | 1.8 |
# | 2 | 5.0 | 4.7 | 5.3 |
# | 3 | 9.0 | 4.7 | 12.3 |
# | 4 | 11.0 | 7.0 | 14.0 |
# | 5 | 6.0 | 9.3 | 3.5 |
# | 6 | 10.0 | 2.3 | 15.8 A |
# | 7 | 13.0 | 16.3 | 10.5 |
# | 8 | 14.0 | 16.3 | 12.3 |
# | 9 | 10.0 | 11.6 | 8.8 |
# | 10 | 10.0 | 9.3 | 10.5 |
# | 99 | 7.0 | 9.3 | 5.3 |
# | Bottom 3 Box | 19.0 | 18.6 | 19.3 |
# | Top 3 Box | 47.0 | 53.5 | 42.1 |
# | #Total cases | 100 | 43 | 57 |