我想在 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 ???
)
uj5u.com熱心網友回復:
您需要構建兩個單獨的表,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.
| 特征 | 4 | 6 | 8 |
|---|---|---|---|
| 英里 | 26.0 (22.8, 30.4) | 19.7 (18.6, 21.0) | 15.2 (14.4, 16.2) |
| 齒輪 | |||
| 3 | 1 (11%) | 2 (33%) | 12 (100%) |
| 4 | 8 (89%) | 4 (67%) | 0 (0%) |
由reprex 包(v2.0.1)于 2021 年 10 月 25 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/336892.html
