向gtsummary tbl_summary添加行和列合计

icnyk63a  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(120)

我使用dplyrgtsummary为列中的多个变量生成一个交叉表,使用以下数据:

# Generate data
set.seed(123)
member <- sample(c("Yes", "No"), 100, replace = TRUE)
author <- sample(c("Yes", "No"), 100, replace = TRUE)
review <- sample(0:10, 100, replace = TRUE)
publish <- sample(0:10, 100, replace = TRUE)
pay <- sample(0:10, 100, replace = TRUE)
data <- data.frame(member, author, review, publish, pay)

我试着使用这个代码,但缺少的是列总数

data %>%
  mutate(across(c(review, publish, pay), ~cut(., breaks = c(-Inf, 4.5, 5.5, Inf),
                                              labels = c("No", "Maybe", "Yes"),
                                              include.lowest = TRUE), .names = "{.col}_group")) %>% 
  select(member, author, ends_with("group")) %>%
  tbl_summary(
    by = member,
    missing = "no", 
    statistic = list(all_categorical() ~ "{n} ({p}%)"),
    digits = list(all_categorical() ~ c(0, 1))
  ) %>%
  add_p(test = all_categorical() ~ "fisher.test")%>% 
  add_overall() %>% 
  #add_n() %>% 
  bold_labels()

我只想为每个变量添加下面的“总体”,“否”和“是”的总和。我尝试了tbl_cross,但是id不允许在列中有多个变量。

tkclm6bt

tkclm6bt1#

我无法在一个tbl_summary()中添加总计行。
因此,我为总计行创建了另一个表,并使用tbl_stack()合并两个表。

library(dplyr)
library(gtsummary)
set.seed(123)
member <- sample(c("Yes", "No"), 100, replace = TRUE)
author <- sample(c("Yes", "No"), 100, replace = TRUE)
review <- sample(0:10, 100, replace = TRUE)
publish <- sample(0:10, 100, replace = TRUE)
pay <- sample(0:10, 100, replace = TRUE)
data <- data.frame(member, author, review, publish, pay)

tbl <- data %>%
  mutate(across(c(review, publish, pay), ~cut(., breaks = c(-Inf, 4.5, 5.5, Inf),
                                              labels = c("No", "Maybe", "Yes"),
                                              include.lowest = TRUE), .names = "{.col}_group")) %>% 
  select(member, author, ends_with("group")) %>%
  tbl_summary(
    by = member,
    missing = "no", 
    statistic = list(all_categorical() ~ "{n} ({p}%)"),
    digits = list(all_categorical() ~ c(0, 1))
  ) %>%
  add_p(test = all_categorical() ~ "fisher.test")%>% 
  add_overall() %>% 

  bold_labels()

tbl_total <- data %>% 
  select(member, author, ends_with("group")) %>%
  mutate(all_true = "Total") %>% 
  tbl_summary(
    by = member,
    include = all_true
  ) %>%
  bold_levels() %>%
  add_overall() %>%
  modify_table_body(
    ~.x %>% filter(row_type %in% "level")
  ) %>%
  modify_column_indent(columns = label, undo = TRUE) 

tbl_stack(list(tbl_total, tbl)) %>%
  as_kable() # only for showing the table in stack overflow
特性总体,N = 100,N = 43,N = 57p值
合计100(100%)43(100%)57(100%)
作者**46人(46.0%)20人(46.5%)26人(45.6%)>0.9
review_group0.2
没有37人(37.0%)20人(46.5%)17人(29.8%)
也许吧7人(7.0%)3人(7.0%)4人(7.0%)
是的56(56.0%)20人(46.5%)36人(63.2%)
publish_group零点零五
没有51人(51.0%)26人(60.5%)25人(43.9%)
也许吧8人(8.0%)5人(11.6%)3人(5.3%)
是的41人(41.0%)12人(27.9%)29人(50.9%)
pay_group0.2
没有41人(41.0%)19人(44.2%)22人(38.6%)
也许吧9人(9.0%)6人(14.0%)3人(5.3%)
是的50(50.0%)18人(41.9%)32人(56.1%)

创建于2023-05-14带有reprex v2.0.2

相关问题