3

我有一个包含很大一部分未知数的连续变量。我的顾问要求我将百分比放在旁边的列中。这个代表模仿了我正在尝试做的事情。

library(tidyverse)
library(gtsummary)

  trial %>%       # included with gtsummary package
  select(trt, age, grade) %>%
  tbl_summary()

我试图将未知数的百分比列在未知数旁边,最好放在括号中。它看起来像 11 (5.5%)。

有些人回复了关于丢失数据如何出现在我的数据集中的请求,这是一个代表

library(gtsummary)
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.0.3
#> Warning: package 'readr' was built under R version 4.0.3
library(gtsummary)

df<-
  tibble::tribble(
               ~age,       ~sex,  ~race,          ~weight,
  70, "male",  "white",       50,
  57, "female", "african-american",   87,
  64,  "male",  "white",              NA,
  46,  "male",  "white", 49,
  87,  "male",  "hispanic", 51
  )

df %>%
  select(age,sex,race,weight) %>%
  tbl_summary(type = list(age ~ "continuous", weight ~ "continuous"), missing="ifany")
4

1 回答 1

4

有几种方法可以报告缺失率。我将在下面举例说明,您可能会选择最适合您的解决方案。

  1. 分类变量:我建议您在将数据框传递给tbl_summary(). NA 值将不再丢失,并将像变量的任何其他级别一样计入。
  2. 连续变量:使用statistic=参数报告缺失率。
  3. 所有变量:add_n()用于报告缺失率
library(gtsummary)

trial %>%      
  select(age, response, trt) %>%
  # making the NA value explicit level of factor with `forcats::fct_explicit_na()`
  dplyr::mutate(response = factor(response) %>% forcats::fct_explicit_na()) %>%
  tbl_summary(
    by = trt,
    type = all_continuous() ~ "continuous2",
    statistic = all_continuous() ~ c("{N_nonmiss}/{N_obs} {p_nonmiss}%",
                                     "{median} ({p25}, {p75})")
  ) %>%
  add_n(statistic = "{n} / {N}")

在此处输入图像描述

编辑:在原始海报评论后添加更多示例。

library(gtsummary)

trial %>%      
  select(age, response, trt) %>%
  # making the NA value explicit level of factor with `forcats::fct_explicit_na()`
  dplyr::mutate(response = factor(response) %>% forcats::fct_explicit_na(na_level = "Unknown")) %>%
  tbl_summary(
    by = trt,
    type = all_continuous() ~ "continuous2",
    missing = "no",
    statistic = all_continuous() ~ c("{median} ({p25}, {p75})",
                                     "{N_miss} ({p_miss}%)")
  ) %>%
  # udpating the Unknown label in the `.$table_body`
  modify_table_body(
    dplyr::mutate,
    label = ifelse(label == "N missing (% missing)",
                   "Unknown",
                   label)
  )

在此处输入图像描述

于 2020-12-22T20:01:50.067 回答