I have been running tables including tab_stat_cpct hoping to have col %'s appearing in the bottom row adding up to 100% in each column. But this is what I get...
mtcars %>%
tab_cells(am) %>%
tab_cols(total(),vs) %>%
tab_stat_cases(label = "N") %>%
tab_stat_cpct(label="%") %>%
tab_pivot(stat_position = "inside_rows") %>% drop_rc() %>%
split_table_to_df()
1 #Total vs
2 0 1
3 am 0 N 19 12 7
4 % 59 67 50
5 1 N 13 6 7
6 % 41 33 50
7 #Total cases N 32 18 14
8 % 32 18 14
They just repeat the total n of cases in each column, not the %. Is this a bug or am I doing something wrong in my code? Thanks.
label
argument is just a label for current block. By default all totals are unweighted number of cases and don't depend on label
. To get desired results we need to set total statistics and total labels:
library(expss)
mtcars %>%
tab_cells(am) %>%
tab_cols(total(),vs) %>%
tab_stat_cases(label = "N", total_label = "") %>%
tab_stat_cpct(label="%", total_statistic = "w_cpct", total_label = "") %>%
tab_pivot(stat_position = "inside_rows")
# | | | | #Total | vs | |
# | | | | | 0 | 1 |
# | -- | -- | -- | ------ | ------ | --- |
# | am | 0 | N | 19.00 | 12.00 | 7 |
# | | | % | 59.38 | 66.67 | 50 |
# | | 1 | N | 13.00 | 6.00 | 7 |
# | | | % | 40.62 | 33.33 | 50 |
# | | # | N | 32.00 | 18.00 | 14 |
# | | | % | 100.00 | 100.00 | 100 |