Mat*_*ica 3 r categorical-data dplyr gtsummary
在下面的数据框中,g 变量有两个级别,但是 tbl_summary() 没有显示其级别。
data.frame(a=c(0,1,2),
b=c(0,1,2),
f=c("m", "f", "m"),
g = c("Yes", "No", "Yes"),
output = c(0,1,0)) %>%
tbl_summary(by=output)
a b f g output
1 0 0 m Yes 0
2 1 1 f No 1
3 2 2 m Yes 0
Run Code Online (Sandbox Code Playgroud)
我尝试遵循R gtsummary 包并没有在汇总表中显示因子级别,但不幸的是我无法解决这个问题。我会很感激任何提示或帮助吗?
您可以使用typeintbl_summary来指定如何显示它。
对于特定列:
library(gtsummary)
df |> tbl_summary(by = output, type = list(g ~ "categorical"))
Run Code Online (Sandbox Code Playgroud)
所有“是”/“否”列:
library(gtsummary)
library(stringr)
names_of_yes_no_columns <- names(Filter(function(x) all(str_detect(x, "Yes|No")), df))
df |> tbl_summary(by = output, type = list(names_of_yes_no_columns ~ "categorical"))
Run Code Online (Sandbox Code Playgroud)
所有二分变量(@Daniel D. Sjoberg):
library(gtsummary)
df |> tbl_summary(by = output, type = all_dichotomous() ~ "categorical")
Run Code Online (Sandbox Code Playgroud)
输出: