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"))