如何在 gtsummary 中对变量进行子集化
How to subset variable in gtsummary
我想在 gtsummary 中对特定变量(不是整个数据集)进行子集化。
在下面的示例中,我如何对档位进行子集化以删除“5”- 只显示档位为“3”和“4”的汽车的比例?但是,我想将所有患者都包括在 mpg 中。
library(gt)
library(dplyr)
mtcars %>%
select(cyl, mpg, gear) %>%
tbl_summary(
by = cyl ### how do i say for gear, filter gear != 5 ???
)
您需要使用 tbl_summary()
构建两个单独的表,然后将它们堆叠起来。示例如下!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'
tbl_full_data <-
mtcars %>%
select(cyl, mpg) %>%
tbl_summary(by = cyl) %>%
# removing Ns from header, since they won't be correct for gear
modify_header(all_stat_cols() ~ "**{level}**")
tbl_gear_subset <-
mtcars %>%
select(cyl, gear) %>%
dplyr::filter(gear != 5) %>%
tbl_summary(by = cyl)
# stack tables together
list(tbl_full_data, tbl_gear_subset) %>%
tbl_stack() %>%
as_kable() # convert to kable to it'll print on SO
#> i Column headers among stacked tables differ. Headers from the first table are
#> used. Use `quiet = TRUE` to supress this message.
Characteristic
4
6
8
mpg
26.0 (22.8, 30.4)
19.7 (18.6, 21.0)
15.2 (14.4, 16.2)
gear
3
1 (11%)
2 (33%)
12 (100%)
4
8 (89%)
4 (67%)
0 (0%)
由 reprex package (v2.0.1)
于 2021-10-25 创建
我想在 gtsummary 中对特定变量(不是整个数据集)进行子集化。
在下面的示例中,我如何对档位进行子集化以删除“5”- 只显示档位为“3”和“4”的汽车的比例?但是,我想将所有患者都包括在 mpg 中。
library(gt)
library(dplyr)
mtcars %>%
select(cyl, mpg, gear) %>%
tbl_summary(
by = cyl ### how do i say for gear, filter gear != 5 ???
)
您需要使用 tbl_summary()
构建两个单独的表,然后将它们堆叠起来。示例如下!
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.0'
tbl_full_data <-
mtcars %>%
select(cyl, mpg) %>%
tbl_summary(by = cyl) %>%
# removing Ns from header, since they won't be correct for gear
modify_header(all_stat_cols() ~ "**{level}**")
tbl_gear_subset <-
mtcars %>%
select(cyl, gear) %>%
dplyr::filter(gear != 5) %>%
tbl_summary(by = cyl)
# stack tables together
list(tbl_full_data, tbl_gear_subset) %>%
tbl_stack() %>%
as_kable() # convert to kable to it'll print on SO
#> i Column headers among stacked tables differ. Headers from the first table are
#> used. Use `quiet = TRUE` to supress this message.
Characteristic | 4 | 6 | 8 |
---|---|---|---|
mpg | 26.0 (22.8, 30.4) | 19.7 (18.6, 21.0) | 15.2 (14.4, 16.2) |
gear | |||
3 | 1 (11%) | 2 (33%) | 12 (100%) |
4 | 8 (89%) | 4 (67%) | 0 (0%) |
由 reprex package (v2.0.1)
于 2021-10-25 创建