gtsummary - 分别显示 unknown/missing 值的比例

gtsummary - present proportion of unknown/missing values separately

我有一个具有未知值的分类变量(种族),我试图用 gtsummary 呈现它,以便分别计算已知值和未知值的比例

我已经到达 this stage

但是我找不到单独计算缺失值比例的方法

我的代码是这样的

trial %>%      
  select(age, response, trt) %>%
  tbl_summary(
    by = trt,
    missing = "if_any",
     statistic = list(all_continuous() ~ "{mean} ({sd})",
                               response~ c("{n} ({p}%)",
                                        "{N_miss} ({p_miss})"),
                               all_categorical() ~ "{n} ({p}%)"),
  ) 

我确实尝试了建议的解决方案 (即 fct_replace_na 和设置 missing = "no"),但它始终在整体比例中包含未知行

谢谢

这是一个很好的问题,我想我应该实施一些措施来简化这个问题。无论如何,我将如何处理: 1. 定义一个新变量,指示该变量是否丢失。 2. 在 table 中汇总此变量并将默认标签更新为未知,3. 缩进缺失的行。

示例如下!

library(gtsummary)
library(dplyr, warn.conflicts = FALSE)
packageVersion("gtsummary")
#> [1] '1.6.0'


tbl <-
  trial %>%      
  mutate(across(c(age, response), is.na, .names = "{.col}_missing")) %>%
  select(age, age_missing, response, response_missing, trt) %>%
  tbl_summary(
    by = trt,
    missing = "no",
    label = ends_with("_missing") ~ "Unknown",
    statistic = list(all_continuous() ~ "{mean} ({sd})",
                     response~ c("{n} ({p}%)",
                                 "{N_miss} ({p_miss})"),
                     all_categorical() ~ "{n} ({p}%)"),
  ) %>%
  modify_column_indent(columns = label, rows = endsWith(variable, "_missing"))

reprex package (v2.0.1)

创建于 2022-04-27