Closed robertogilsaura closed 2 months ago
Hi, @robertogilsaura
It simpler to modify resulted table. Something like this:
library(expss)
tbl = mtcars %>%
tab_cols(vs) %>%
tab_cells(hp, gear) %>%
tab_stat_cases(label = "") %>%
tab_stat_cpct(label = "%") %>%
tab_pivot(stat_position = "inside_columns")
tbl
# | | | vs | | | |
# | | | 0 | | 1 | |
# | | | | % | | % |
# | ---- | ------------ | -- | ---- | -- | ---- |
# | hp | 52 | | | 1 | 7.1 |
# | | 62 | | | 1 | 7.1 |
# | | 65 | | | 1 | 7.1 |
# | | 66 | | | 2 | 14.3 |
# | | 91 | 1 | 5.6 | | |
# | | 93 | | | 1 | 7.1 |
# | | 95 | | | 1 | 7.1 |
# | | 97 | | | 1 | 7.1 |
# | | 105 | | | 1 | 7.1 |
# | | 109 | | | 1 | 7.1 |
# | | 110 | 2 | 11.1 | 1 | 7.1 |
# | | 113 | | | 1 | 7.1 |
# | | 123 | | | 2 | 14.3 |
# | | 150 | 2 | 11.1 | | |
# | | 175 | 3 | 16.7 | | |
# | | 180 | 3 | 16.7 | | |
# | | 205 | 1 | 5.6 | | |
# | | 215 | 1 | 5.6 | | |
# | | 230 | 1 | 5.6 | | |
# | | 245 | 2 | 11.1 | | |
# | | 264 | 1 | 5.6 | | |
# | | 335 | 1 | 5.6 | | |
# | | #Total cases | 18 | 18.0 | 14 | 14.0 |
# | gear | 3 | 12 | 66.7 | 3 | 21.4 |
# | | 4 | 2 | 11.1 | 10 | 71.4 |
# | | 5 | 4 | 22.2 | 1 | 7.1 |
# | | #Total cases | 18 | 18.0 | 14 | 14.0 |
single_column_stat = function(tbl){
stat_cols_1 = seq(from = 2, to = ncol(tbl), by =2)
stat_cols_2 = seq(from = 3, to = ncol(tbl), by =2)
for(i in stat_cols_1){
tbl[,i] = ifelse(!is.na(tbl[[i]]), paste0(tbl[[i]], ' (% ', round(tbl[[i+1]], 0), ')'), NA)
}
tbl[,c(1, stat_cols_1)]
}
single_column_stat(tbl)
# | | | vs | |
# | | | 0 | 1 |
# | ---- | ------------ | --------- | --------- |
# | hp | 52 | | 1 (% 7) |
# | | 62 | | 1 (% 7) |
# | | 65 | | 1 (% 7) |
# | | 66 | | 2 (% 14) |
# | | 91 | 1 (% 6) | |
# | | 93 | | 1 (% 7) |
# | | 95 | | 1 (% 7) |
# | | 97 | | 1 (% 7) |
# | | 105 | | 1 (% 7) |
# | | 109 | | 1 (% 7) |
# | | 110 | 2 (% 11) | 1 (% 7) |
# | | 113 | | 1 (% 7) |
# | | 123 | | 2 (% 14) |
# | | 150 | 2 (% 11) | |
# | | 175 | 3 (% 17) | |
# | | 180 | 3 (% 17) | |
# | | 205 | 1 (% 6) | |
# | | 215 | 1 (% 6) | |
# | | 230 | 1 (% 6) | |
# | | 245 | 2 (% 11) | |
# | | 264 | 1 (% 6) | |
# | | 335 | 1 (% 6) | |
# | | #Total cases | 18 (% 18) | 14 (% 14) |
# | gear | 3 | 12 (% 67) | 3 (% 21) |
# | | 4 | 2 (% 11) | 10 (% 71) |
# | | 5 | 4 (% 22) | 1 (% 7) |
# | | #Total cases | 18 (% 18) | 14 (% 14) |
Thanks @gdemin, my approach was not the right one.
Best regards ...
Hi @gdemin, I hope your summer have been extremely productive.
I would like to use the tab_stat_fun() or tab_stat_fun_df() function to be able to obtain the value of cases and the % of cases in the same cell of a table. For example: 20 (15%)
Do you see it possible with these functions? I have tried with this table, trying to use tab_stat_cases() and tab_stat_cpct() in the function declared in tab_stat_fun_df() and with u_cases and u_cpct in the parameters of tab_stat_fun() and neither of the two have been valid (with errors for my coding)...
The base table would be (for example) ...
The rows marked by
-
should be modified to obtain the value: n (% n)Thanks in advance...