Home > front end >  Is possible to do a table with two group by with tbl_summary in R?
Is possible to do a table with two group by with tbl_summary in R?

Time:01-05

I'm working with the mtcars database, and I'm using the tbl_summary function. What I'm trying to do is to have two group by's, first with the type of transmission and then with the number of cylinders, so I will have six columns in total for the group by's plus the overall column, until now I have only been able to do the group by with one variable only.

This is my code:

mtcars2 <- within(mtcars, {
  vs <- factor(vs, labels = c("V", "S"))
  am <- factor(am, labels = c("Automatic", "Manual"))
  cyl  <- ordered(cyl)
  gear <- ordered(gear)
  carb <- ordered(carb)
})

mtcars2 %>%
  tbl_summary(
    by = cyl,
    type = all_continuous() ~ "continuous2",
    statistic = list(all_continuous() ~ c("{mean} ({sd})",
                                          "{min}, {max}",
                                          "{skew}"),
                     all_categorical() ~ "{n} / {N} ({p}%)"),
    digits = all_continuous() ~ 1,
    label = list(mpg ~ "Miles/ Gallon", disp ~ "Displacement (cu.in.)", hp ~ "Gross Horsepower", drat ~ "Rear Axle Ratio", wt ~ "Weight (1,000 lbs)", qsec ~ "1/4 Mile Time", vs ~ "Engine (Shape)", am ~ "Transmission", gear ~ "No. of Forward Gears", carb ~ "No. of Carburetors")
  ) %>%
  add_overall() %>%
  modify_header(label ~ "**Variable**") %>%
  modify_spanning_header(c("stat_1", "stat_2", "stat_3") ~ "**Number of Cylinders**") %>%
  modify_caption("**Table 1. Descriptive Statistics**")  %>%
  add_stat_label(label = all_continuous() ~ c("Mean (SD)", "Range", "Skew"))

CodePudding user response:

You can use the tbl_strata() function to stratify the tbl_summary() by a second by variable. Example below!

library(gtsummary)

tbl <- 
  mtcars %>%
  select(am, cyl, mpg, hp) %>%
  dplyr::mutate(
    cyl = paste(cyl, "Cylinder"),
    am = factor(am, labels = c("Automatic", "Manual"))
  ) %>%
  tbl_strata(
    strata = cyl,
    ~.x %>%
      tbl_summary(
        by = am,
        type = where(is.numeric) ~ "continuous"
      ) %>%
      modify_header(all_stat_cols() ~ "**{level}**")
  )

enter image description here Created on 2022-01-03 by the reprex package (v2.0.1)

  •  Tags:  
  • Related