在 tbl_regression 中重命名一个因素的水平
Rename levels of a factor in tbl_regression
我想在 tbl_regression
内重命名一个因子的级别而不更改我的数据框。我了解如何使用 label
在 tbl_regression
中重命名变量名称,但是是否可以使用 label
来编辑因子水平?
library(tidyverse)
library(gtsummary)
glm(response ~ trt + factor(death), data = trial) %>%
tbl_regression(
label = list(
trt ~ "Drug B vs A",
`factor(death)` ~ "Death" ) <- # how to change 0/1 to alive/dead?
)
结果:
Characteristic Beta 95% CI p-value
─────────────────────────────────────────────────────
Drug B vs A
Drug A — —
Drug B 0.06 -0.07, 0.19 0.4
Death
0 — —
1 -0.21 -0.34, -0.08 0.002
─────────────────────────────────────────────────────
CI = Confidence Interval
期望的结果:
Characteristic Beta 95% CI p-value
─────────────────────────────────────────────────────
Drug B vs A
Drug A — —
Drug B 0.06 -0.07, 0.19 0.4
Death
Alive — —
Dead -0.21 -0.34, -0.08 0.002
─────────────────────────────────────────────────────
CI = Confidence Interval
您可以使用modify_table_body()
更改关卡的标签
glm(response ~ trt + factor(death), data = trial) %>%
tbl_regression(
label = list(
trt ~ "Drug B vs A",
`factor(death)` ~ "Death" )
) %>%
modify_table_body(
~.x %>%
mutate(label = ifelse(label == "0", "Alive",
ifelse(label =="1", "Dead",label)))
)
如果您想对更改的标签更加谨慎,可以在 ifelse()
语句中添加另一个条件:
glm(response ~ trt + factor(death), data = trial) %>%
tbl_regression(
label = list(
trt ~ "Drug B vs A",
`factor(death)` ~ "Death" )
) %>%
modify_table_body(
~.x %>%
mutate(label = ifelse(label == "0" & variable == "factor(death)", "Alive",
ifelse(label =="1" & variable == "factor(death)", "Dead",label)))
)
我想在 tbl_regression
内重命名一个因子的级别而不更改我的数据框。我了解如何使用 label
在 tbl_regression
中重命名变量名称,但是是否可以使用 label
来编辑因子水平?
library(tidyverse)
library(gtsummary)
glm(response ~ trt + factor(death), data = trial) %>%
tbl_regression(
label = list(
trt ~ "Drug B vs A",
`factor(death)` ~ "Death" ) <- # how to change 0/1 to alive/dead?
)
结果:
Characteristic Beta 95% CI p-value
─────────────────────────────────────────────────────
Drug B vs A
Drug A — —
Drug B 0.06 -0.07, 0.19 0.4
Death
0 — —
1 -0.21 -0.34, -0.08 0.002
─────────────────────────────────────────────────────
CI = Confidence Interval
期望的结果:
Characteristic Beta 95% CI p-value
─────────────────────────────────────────────────────
Drug B vs A
Drug A — —
Drug B 0.06 -0.07, 0.19 0.4
Death
Alive — —
Dead -0.21 -0.34, -0.08 0.002
─────────────────────────────────────────────────────
CI = Confidence Interval
您可以使用modify_table_body()
更改关卡的标签
glm(response ~ trt + factor(death), data = trial) %>%
tbl_regression(
label = list(
trt ~ "Drug B vs A",
`factor(death)` ~ "Death" )
) %>%
modify_table_body(
~.x %>%
mutate(label = ifelse(label == "0", "Alive",
ifelse(label =="1", "Dead",label)))
)
如果您想对更改的标签更加谨慎,可以在 ifelse()
语句中添加另一个条件:
glm(response ~ trt + factor(death), data = trial) %>%
tbl_regression(
label = list(
trt ~ "Drug B vs A",
`factor(death)` ~ "Death" )
) %>%
modify_table_body(
~.x %>%
mutate(label = ifelse(label == "0" & variable == "factor(death)", "Alive",
ifelse(label =="1" & variable == "factor(death)", "Dead",label)))
)