0

I have a table based on the mtcars dataset. In column, I have both variables engine and transmissions; in the row I have the variable cylinder number. I would like to have, instead of 100, the number of cases per row or the number of cases for each number of cylinders

multiple banners tres important

library(expss)
data(mtcars)
mtcars %>% 
    tab_cells(cyl) %>% 
    tab_cols(vs,total(),am,total("u_cases"),vs,total()) %>% 
    tab_total_row_position("below") %>% 
    tab_total_statistic("u_rpct")%>%
    tab_stat_rpct() %>% 
    tab_pivot()
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20

1 Answers1

0

If I correctly understand what do you need:

mtcars %>% 
    tab_cells(cyl) %>% 
    tab_total_row_position("below") %>% 
    tab_total_statistic("u_rpct")%>%
    tab_cols(vs) %>% 
    tab_stat_rpct() %>% 
    tab_cols(total(vs)) %>% 
    tab_stat_cases() %>% 
    tab_cols(am) %>% 
    tab_stat_rpct() %>% 
    tab_cols(total(am)) %>% 
    tab_stat_cases() %>% 
    tab_pivot(stat_position = "outside_columns")

# |     |             |    vs |      | #Total |   am |      | #Total |
# |     |             |     0 |    1 |        |    0 |    1 |        |
# | --- | ----------- | ----- | ---- | ------ | ---- | ---- | ------ |
# | cyl |           4 |   9.1 | 90.9 |     11 | 27.3 | 72.7 |     11 |
# |     |           6 |  42.9 | 57.1 |      7 | 57.1 | 42.9 |      7 |
# |     |           8 | 100.0 |      |     14 | 85.7 | 14.3 |     14 |
# |     | #Total rpct |  56.2 | 43.8 |    100 | 59.4 | 40.6 |    100 |
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • **I thank you for this table which is close to what I want to have as a result. I would like to add the total after each variable (am and vs) because if we take into account nonresponse cases, each category of the cylinder variable may not have the same total for both variables (VS and AM)** Below is a sample copy of a table. – Legarraudien Oct 13 '19 at 00:17