1

I want to create a table with the expss package where I show several variables in row with the column percentages. I want to add ONE total statistic at the bottom, however, whatever I'm trying, expss is always giving me the total statistic for each variable.

E.g. this:

mtcars |> 
    expss::tab_cols(list(am, expss::total())) |>
    expss::tab_cells(cyl, gear) |> 
    expss::tab_stat_cpct(total_row_position = "none") |>
    expss::tab_stat_cases(total_statistic = c("u_cases", "w_cases"), total_label = c("N (unweighted)", "N (weighted)")) |> 
    expss::tab_pivot()

gives this:

 |      |                 |    0 |    1 | #Total |
 | ---- | --------------- | ---- | ---- | ------ |
 |  cyl |               4 | 15.8 | 61.5 |   34.4 |
 |      |               6 | 21.1 | 23.1 |   21.9 |
 |      |               8 | 63.2 | 15.4 |   43.8 |
 | gear |               3 | 78.9 |      |   46.9 |
 |      |               4 | 21.1 | 61.5 |   37.5 |
 |      |               5 |      | 38.5 |   15.6 |
 |  cyl |               4 |  3.0 |  8.0 |   11.0 |
 |      |               6 |  4.0 |  3.0 |    7.0 |
 |      |               8 | 12.0 |  2.0 |   14.0 |
 |      | #N (unweighted) | 19.0 | 13.0 |   32.0 |
 |      |   #N (weighted) | 19.0 | 13.0 |   32.0 |
 | gear |               3 | 15.0 |      |   15.0 |
 |      |               4 |  4.0 |  8.0 |   12.0 |
 |      |               5 |      |  5.0 |    5.0 |
 |      | #N (unweighted) | 19.0 | 13.0 |   32.0 |
 |      |   #N (weighted) | 19.0 | 13.0 |   32.0 |

However, what I want is this:

 |      |                 |    0 |    1 | #Total |
 | ---- | --------------- | ---- | ---- | ------ |
 |  cyl |               4 | 15.8 | 61.5 |   34.4 |
 |      |               6 | 21.1 | 23.1 |   21.9 |
 |      |               8 | 63.2 | 15.4 |   43.8 |
 | gear |               3 | 78.9 |      |   46.9 |
 |      |               4 | 21.1 | 61.5 |   37.5 |
 |      |               5 |      | 38.5 |   15.6 |
 |      | #N (unweighted) | 19.0 | 13.0 |   32.0 |
 |      |   #N (weighted) | 19.0 | 13.0 |   32.0 |
deschen
  • 10,012
  • 3
  • 27
  • 50

1 Answers1

1

Possible solution:

mtcars |> 
    expss::tab_cols(list(am, expss::total())) |>
    expss::tab_cells(cyl, vs) |> # any number of variables without total
    expss::tab_total_row_position("none") |>
    expss::tab_stat_cpct() |>
    expss::tab_cells(gear) |> # last variable with total
    expss::tab_total_label(c("N (unweighted)", "N (weighted)")) |> 
    expss::tab_total_row_position("below") |>
    expss::tab_stat_cpct(total_statistic = c("u_cases", "w_cases")) |> 
    expss::tab_pivot()

# |      |                 |    0 |    1 | #Total |
# | ---- | --------------- | ---- | ---- | ------ |
# |  cyl |               4 | 15.8 | 61.5 |   34.4 |
# |      |               6 | 21.1 | 23.1 |   21.9 |
# |      |               8 | 63.2 | 15.4 |   43.8 |
# |   vs |               0 | 63.2 | 46.2 |   56.2 |
# |      |               1 | 36.8 | 53.8 |   43.8 |
# | gear |               3 | 78.9 |      |   46.9 |
# |      |               4 | 21.1 | 61.5 |   37.5 |
# |      |               5 |      | 38.5 |   15.6 |
# |      | #N (unweighted) | 19.0 | 13.0 |   32.0 |
# |      |   #N (weighted) | 19.0 | 13.0 |   32.0 |
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • So you basically split the table into two parts. Any number of vars for which we just calculate %. And then we do one last variable for which we get the total? – deschen Oct 17 '22 at 05:38
  • 1
    Would be maybe interesting to have a dedicated function for it instead of this bit of coding gymnastic. Sth. like `add_table_totals`? – deschen Oct 17 '22 at 05:39
  • @deschen The issue here is that total counts only non-missing cases. So we can have different total values for different variables. Thus, common totals can be very misleading. Perhaps, a sensible algorithm would be to remove the same totals but it can't guarantee a single total for the table. – Gregory Demin Oct 18 '22 at 17:55
  • Yes, the restriction is that all variables have a common base. But omitting all the totals and just add one at the bottom has the same restriction, i.e. all vars have the same base. So if someone is running such a „add_totals“ command, they are hopefully and should be aware of this potential issue. Or maybe when such a finction is run it could either error or warn if there are different totals. – deschen Oct 18 '22 at 18:30