可以用 R 中的 tbl_summary 和两个组做一个 table 吗?
Is possible to do a table with two group by with tbl_summary in R?
我正在使用 mtcars 数据库,并且正在使用 tbl_summary 函数。我想做的是有两个分组依据,首先是变速器类型,然后是气缸数,所以我总共有六列用于分组依据加上整体列,直到现在我只有只能用一个变量进行分组。
这是我的代码:
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"))
您可以使用tbl_strata()
函数按变量对tbl_summary()
进行秒级分层。示例如下!
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}**")
)
由 reprex package (v2.0.1)
创建于 2022-01-03
我正在使用 mtcars 数据库,并且正在使用 tbl_summary 函数。我想做的是有两个分组依据,首先是变速器类型,然后是气缸数,所以我总共有六列用于分组依据加上整体列,直到现在我只有只能用一个变量进行分组。
这是我的代码:
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"))
您可以使用tbl_strata()
函数按变量对tbl_summary()
进行秒级分层。示例如下!
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}**")
)