Error while using mutate with tbl_strata and tbl_summary in R

2.2k views Asked by At

I'm trying to do a summary table with two group variables (am and cyl), so I would have six columns plus the overall, I have the following 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 %>%
  select(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb) %>%
  dplyr::mutate(
    cyl = paste(cyl, "Cylinder"), 
    am = factor(am, labels = c("Automatic", "Manual"))
  ) %>%
  tbl_strata(
    strata = am, .tbl_fun = 
    ~.x %>%
  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")) %>%
  bold_labels())

But I'm getting the following error:

Error: Problem with `mutate()` column `tbl`.
i `tbl = map(.data$data, .tbl_fun, ...)`.
x Error in `label=` argument input. Select from ‘mpg’, ‘cyl’, ‘disp’, ‘hp’, ‘drat’, ‘wt’, ‘qsec’, ‘vs’, ‘gear’, ‘carb’
1

There are 1 answers

1
TarJae On

Replace the ~ with a = in the label argument of tbl_summary:

mtcars2 %>%
  select(mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb) %>%
  dplyr::mutate(
    cyl = paste(cyl, "Cylinder"), 
    am = factor(am, labels = c("Automatic", "Manual"))
  ) %>%
  tbl_strata(
    strata = am, .tbl_fun = 
      ~.x %>%
      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")) %>%
      bold_labels())

enter image description here