How to use datasummary_skim() on groups in R?

122 views Asked by At

I like to have the awesome statistics of datasummary_skim() from modelsummary package by groups.

library(modelsummary)
library(tidyverse)

mtcars %>% datasummary_skim()

enter image description here

How can I have a grouped data summary?

mtcars %>% group_by(vs) %>% datasummary_skim()
3

There are 3 answers

3
Mark On

One option is to split the dataframe by groups, and then use map() to apply datasummary_skim() to each child dataframe:

mtcars |>
  group_split(cyl) |>
  map(datasummary_skim)

Output:

table 1 other tables follow

enter image description here

enter image description here

0
Julian Selke On

Here's another solution including group titles and plotting all tables on a scrollable panel:

mtcars %>% 
  group_by(vs) %>% 
  nest() %>% 
  summarize(summary = datasummary_skim(data, title = vs)) %>% 
  pull(summary)

plot

0
Vincent On

Version 2.0.0 of modelsummary includes a by argument in the datasummary_skim() function. You can wait until the new version reaches CRAN, or install the development version from Github:

library(remotes)
install_github("vincentarelbundock/tinytable")
install_github("vincentarelbundock/modelsummary")
library(modelsummary)
x <- mtcars[, c("hp", "mpg", "gear")]

datasummary_skim(x, by = "gear")

enter image description here