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)
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
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** | | | | | | |
#> | min |10.4 |18.1 |21.0 |17.8 |15.0 |30.4 |
#> | max |19.2 |21.5 |21.0 |33.9 |26.0 |30.4 |
#> | mean (sd) |15.05 ± 2.77 |20.33 ± 1.93 |21.00 ± 0.00 |25.24 ± 5.54 |19.12 ± 5.02 |30.40 ± NA |
#> |**Displacement** | | | | | | |
#> | min |275.8 |120.1 |160.0 |71.1 |120.3 |95.1 |
#> | median |355.0 |225.0 |160.0 |114.5 |223.0 |95.1 |
#> | max |472.0 |258.0 |160.0 |167.6 |351.0 |95.1 |
#> | mean (sd) |357.62 ± 71.82 |201.03 ± 72.01 |160.00 ± 0.00 |115.62 ± 38.54 |229.32 ± 113.93 |95.10 ± NA |
#> |**Weight (1000 lbs)** | | | | | | |
#> | min |3.435 |2.465 |2.620 |1.615 |2.140 |1.513 |
#> | max |5.424 |3.460 |2.875 |3.440 |3.570 |1.513 |
#> | mean (sd) |4.10 ± 0.77 |3.05 ± 0.52 |2.75 ± 0.18 |2.59 ± 0.69 |2.91 ± 0.61 |1.51 ± NA |
#> |**Forward Gears** | | | | | | |
#> | Three |12 (100) |3 (100) |0 (0) |0 (0) |0 (0) |0 (0) |
#> | Four |0 (0) |0 (0) |2 (100) |10 (100) |0 (0) |0 (0) |
#> | Five |0 (0) |0 (0) |0 (0) |0 (0) |4 (100) |1 (100) |