rdataframedplyrmarkdownqwraps2

Apply `qwraps2` on nested groups


With the package qwraps2 we can create nice summary tables in HTML and Latex.

Grouped data.frames with one group are supported as well but what about nested groups? Is there a way to directly pass a data.frame with nested groups to qwraps2::summary_table()?

Alternatively, is there a way to cbind() two outputs?

Consider the following:

R code

library(dplyr)
library(qwraps2)

our_summary1 <-
    list("Miles Per Gallon" =
                 list("min" = ~ min(.data$mpg),
                      "max" = ~ max(.data$mpg),
                      "mean (sd)" = ~ qwraps2::mean_sd(.data$mpg)),
         "Displacement" =
                 list("min" = ~ min(.data$disp),
                      "median" = ~ median(.data$disp),
                      "max" = ~ max(.data$disp),
                      "mean (sd)" = ~ qwraps2::mean_sd(.data$disp)),
         "Weight (1000 lbs)" =
                 list("min" = ~ min(.data$wt),
                      "max" = ~ max(.data$wt),
                      "mean (sd)" = ~ qwraps2::mean_sd(.data$wt)),
         "Forward Gears" =
                 list("Three" = ~ qwraps2::n_perc0(.data$gear == 3),
                      "Four"  = ~ qwraps2::n_perc0(.data$gear == 4),
                      "Five"  = ~ qwraps2::n_perc0(.data$gear == 5))
    )

summary_table(mtcars %>% dplyr::group_by(vs), our_summary1)

output

I would like to group by vs and gear however. Each vs group would consequently have three gear subgroups (gear == 3, 4, or 5).

The following results in an error:

summary_table(mtcars %>% dplyr::group_by(vs, gear), our_summary1)
Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent

Solution

  • You can add a column manually or use interaction(vs, gear) for grouping:

    library(dplyr)
    
    library(qwraps2)
    options(qwraps2_markup = "markdown")
    
    summary_table(mtcars %>% 
                    dplyr::mutate(vsgear = paste0("vs = ", vs, ", gear = ", gear)) %>% 
                    dplyr::group_by(vsgear), our_summary1)
    
    summary_table(mtcars %>% dplyr::group_by(interaction(vs, gear, sep=",")), our_summary1)
    
    ###showing results for the second option only
    
    #> 
    #> |                       |interaction(vs, gear): 0,3 (N = 12) |interaction(vs, gear): 1,3 (N = 3) |interaction(vs, gear): 0,4 (N = 2) |interaction(vs, gear): 1,4 (N = 10) |interaction(vs, gear): 0,5 (N = 4) |interaction(vs, gear): 1,5 (N = 1) |
    #> |:----------------------|:-----------------------------------|:----------------------------------|:----------------------------------|:-----------------------------------|:----------------------------------|:----------------------------------|
    #> |**Miles Per Gallon**   |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |
    #> |&nbsp;&nbsp; min       |10.4                                |18.1                               |21.0                               |17.8                                |15.0                               |30.4                               |
    #> |&nbsp;&nbsp; max       |19.2                                |21.5                               |21.0                               |33.9                                |26.0                               |30.4                               |
    #> |&nbsp;&nbsp; mean (sd) |15.05 &plusmn; 2.77                 |20.33 &plusmn; 1.93                |21.00 &plusmn; 0.00                |25.24 &plusmn; 5.54                 |19.12 &plusmn; 5.02                |30.40 &plusmn;  NA                 |
    #> |**Displacement**       |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |
    #> |&nbsp;&nbsp; min       |275.8                               |120.1                              |160.0                              |71.1                                |120.3                              |95.1                               |
    #> |&nbsp;&nbsp; median    |355.0                               |225.0                              |160.0                              |114.5                               |223.0                              |95.1                               |
    #> |&nbsp;&nbsp; max       |472.0                               |258.0                              |160.0                              |167.6                               |351.0                              |95.1                               |
    #> |&nbsp;&nbsp; mean (sd) |357.62 &plusmn; 71.82               |201.03 &plusmn; 72.01              |160.00 &plusmn; 0.00               |115.62 &plusmn; 38.54               |229.32 &plusmn; 113.93             |95.10 &plusmn;  NA                 |
    #> |**Weight (1000 lbs)**  |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |
    #> |&nbsp;&nbsp; min       |3.435                               |2.465                              |2.620                              |1.615                               |2.140                              |1.513                              |
    #> |&nbsp;&nbsp; max       |5.424                               |3.460                              |2.875                              |3.440                               |3.570                              |1.513                              |
    #> |&nbsp;&nbsp; mean (sd) |4.10 &plusmn; 0.77                  |3.05 &plusmn; 0.52                 |2.75 &plusmn; 0.18                 |2.59 &plusmn; 0.69                  |2.91 &plusmn; 0.61                 |1.51 &plusmn;  NA                  |
    #> |**Forward Gears**      |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |&nbsp;&nbsp;                        |&nbsp;&nbsp;                       |&nbsp;&nbsp;                       |
    #> |&nbsp;&nbsp; Three     |12 (100)                            |3 (100)                            |0 (0)                              |0 (0)                               |0 (0)                              |0 (0)                              |
    #> |&nbsp;&nbsp; Four      |0 (0)                               |0 (0)                              |2 (100)                            |10 (100)                            |0 (0)                              |0 (0)                              |
    #> |&nbsp;&nbsp; Five      |0 (0)                               |0 (0)                              |0 (0)                              |0 (0)                               |4 (100)                            |1 (100)                            |