【问题标题】:Is there a way to collapse two columns to one in gtsummary?有没有办法在 gtsummary 中将两列折叠成一列?
【发布时间】:2021-09-17 12:24:46
【问题描述】:

我正在尝试折叠到列以便拥有单个列以适应给定的表结构。所需的输出为 23 (21.2 - 24.5)。我已经创建了两个表,但我似乎没有得到它。本质上,我希望比例和 CI 放在一个只有一个标题的列中 % (95% CI)

library(gtsummary)
library(tidyverse)

# Obtain proportions

tbl1 <-
  trial %>%
  mutate(trt = as.factor(trt)) %>%
  select(grade, response, trt) %>%
  tbl_summary(missing = "no",  type = everything() ~ "categorical",
              statistic = all_categorical() ~ "{p}")

myci2 <-
  tbl1$meta_data %>%
  filter(summary_type %in% c("categorical", "dichotomous")) %>%
  select(summary_type, var_label, df_stats) %>%
  unnest(df_stats) %>%
  mutate(
    conf.low = (p - qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    conf.high =( p + qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    ci = str_glue("{conf.low}, {conf.high}"),
    label = coalesce(variable_levels, var_label),
    row_type = ifelse(summary_type == "dichotomous", "label", "level")
  ) %>%
  select(variable, row_type, label, ci)


finaltbl <-
  tbl1 %>%
  modify_table_body(
    left_join,
    myci2,
    by = c("variable", "row_type", "label")
  ) %>%
  modify_header(ci = "**% (95% CI)**")

finaltbl

finaltbl[["table_body"]] <- finaltbl[["table_body"]] %>%
  mutate(prev = paste0(stat_0, " (", ci, ")")) 


finaltbl %>% 
  modify_column_unhide(prev) %>% 
  modify_column_hide(c(stat_0, ci))


【问题讨论】:

  • 你为什么不把数据传递给gtsummary之前的两列dplyr::unite
  • 我正在尝试,但似乎得到了错误
  • 我正在尝试修改表格正文,但似乎无法正常工作。让我检查 unite(stat_0, ci) 中的错误:找不到对象“stat_0”
  • stat_0 来自哪里?您应该将数据传递给gtsummary 之前对其进行操作。我认为您已经在 trial 数据框(如果那是一个数据框)中获得了您所需要的一切,请将其发布,以便我们查看其中的内容并提出解决方案。

标签: r gtsummary


【解决方案1】:

下面的例子!

library(gtsummary)
library(tidyverse)
#> Warning: package 'readr' was built under R version 4.1.1
packageVersion("gtsummary")
#> [1] '1.4.2.9008'

# Obtain proportions
tbl1 <-
  trial %>%
  mutate(trt = as.factor(trt)) %>%
  select(grade, response, trt) %>%
  tbl_summary(missing = "no",  type = everything() ~ "categorical",
              statistic = all_categorical() ~ "{p}%")

myci2 <-
  tbl1$meta_data %>%
  filter(summary_type %in% c("categorical", "dichotomous")) %>%
  select(summary_type, var_label, df_stats) %>%
  unnest(df_stats) %>%
  mutate(
    conf.low = (p - qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    conf.high =( p + qnorm(0.975) * sqrt(p * (1 - p) / N)) %>%
      style_percent(symbol = TRUE),
    ci = str_glue("{style_percent(p)}% ({conf.low}, {conf.high})"),
    label = coalesce(variable_levels, var_label),
    row_type = ifelse(summary_type == "dichotomous", "label", "level")
  ) %>%
  select(variable, row_type, label, ci)


finaltbl <-
  tbl1 %>%
  modify_table_body(
    left_join,
    myci2,
    by = c("variable", "row_type", "label")
  ) %>%
  modify_header(ci = "**% (95% CI)**") %>%
  modify_column_hide(stat_0)

reprex package (v2.0.1) 于 2021-09-18 创建

【讨论】:

猜你喜欢
  • 2021-10-22
  • 2019-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
相关资源
最近更新 更多