deschen
deschen

Reputation: 10996

expss how to add total statistics only once even if several variables are specified in cells

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 |

Upvotes: 1

Views: 266

Answers (1)

Gregory Demin
Gregory Demin

Reputation: 4846

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 |

Upvotes: 1

Related Questions