If I correctly understand, you need:
mtcars %>%
tab_cells(mpg) %>%
tab_cols(vs, am) %>%
tab_rows(set_var_lab(gear, "gear") %nest% set_var_lab(cyl, "cyl")) %>%
tab_stat_median() %>%
tab_pivot()
It gives:
# | | | | | | | vs | | am | |
# | | | | | | | 0 | 1 | 0 | 1 |
# | ---- | -- | --- | -- | --- | ------ | ---- | ---- | ---- | ---- |
# | gear | 3 | cyl | 4 | mpg | Median | | 21.5 | 21.5 | |
# | | | | 6 | mpg | Median | | 19.8 | 19.8 | |
# | | | | 8 | mpg | Median | 15.2 | | 15.2 | |
# | | 4 | cyl | 4 | mpg | Median | | 25.9 | 23.6 | 28.9 |
# | | | | 6 | mpg | Median | 21.0 | 18.5 | 18.5 | 21.0 |
# | | | | 8 | mpg | Median | | | | |
# | | 5 | cyl | 4 | mpg | Median | 26.0 | 30.4 | | 28.2 |
# | | | | 6 | mpg | Median | 19.7 | | | 19.7 |
# | | | | 8 | mpg | Median | 15.4 | | | 15.4 |
UPDATE:
- tab_rows - row grouping variables
- tab_cols - column grouping variables
- tab_cells - variables on which we calculate statistics. It is rather natural when we calculate summary statistics such as median, mean and etc, but may be confusing when we calculate cases or column percent. You can get some docs by typing
?tab_cells
in the console.
```
| | tab_cols |
|tab_rows | stat(tab_cells)|
```
For count of cars:
mtcars %>%
tab_cells(mpg) %>%
tab_cols(vs, am) %>%
tab_rows(set_var_lab(gear, "gear") %nest% set_var_lab(cyl, "cyl")) %>%
tab_stat_median() %>%
tab_stat_valid_n(label = "#Total") %>%
tab_pivot(stat_position = "inside_rows")
You can manage number of decimals with expss_digits()
but it changes the number of decimals for entire table. Or, if you are using RStudio on Windows you can try expss_output_viewer()
for output in RStudio viewer. In this case rows with "#" will be shown without decimals.