gt_summary::tbl_stack()问题:使用gtreg的tbl_listing()时,表被移位

n3h0vuf2  于 2024-01-03  发布在  其他
关注(0)|答案(2)|浏览(99)

这是一个跟进问题
请考虑以下示例:

library(gtsummary)
library(gtreg)
library(dplyr)
library(tidyr)

df <- structure(
  list(id = c("patient1", "patient2", "patient3", "patient4", 
              "patient5"), x = c("h,a,a", "i", "i", "i,a,e", "h")), 
  class = "data.frame", row.names = c(NA, -5L)
)

df1 <- structure(
  list(id = c("patient1", "patient2", "patient3", "patient4", 
              "patient5"), y = c("yes", "no", "yes", "yes", "no")), 
  class = "data.frame", row.names = c(NA, -5L)
)


# prep data for a table
tbl <- df |> 
  separate_rows(x, sep = ",") |> 
  select(x) |>
  count(x) |> 
  mutate(percent = round(n/nrow(df)*100)) |> 
  mutate(N_percent = paste0(n,"/", nrow(df), " (",percent,"%)"), .keep = "unused") |> 
  select(x, N = N_percent) |> 
  mutate(grouping_var = "x", .before = 1L) |> 
  # create a gtsummary class listing
  tbl_listing(df, group_by = grouping_var) |> 
  # style table with gtusmmary functions to make it look like `tbl_summary()`
  modify_header(
    x ~ "**Characteristic**",
    N ~ "**N = 5**"
  ) |> 
  modify_column_alignment(N, align = "center") |> 
  modify_footnote(N = "n/N (%)")

tbl1 <- df1 |> 
  select(y) |> 
  tbl_summary(
    type = list(y ~ "categorical"))

tbl_stack(
  tbls = list(tbl, tbl1)
  )

字符串
在第二个表中产生移位:


的数据
我会期待这样的事情:


juud5qan

juud5qan1#

正如@丹尼尔D. Sjoberg的宝贵评论(所有功劳都归功于他):
1.用gtreg s show_header_names()检查头名称:

library(gtreg)
show_header_names()

  
show_header_names(tbl, tbl1)

output: 
  Column Name   Column Header      
------------  -------------------
  label         **Characteristic** 
  stat_0        **N = 5**

字符串
现在在tbl中重命名相应的名称:

library(gtsummary)
library(gtreg)
library(dplyr)

tbl <- df |> 
  separate_rows(x, sep = ",") |> 
  select(x) |>
  count(x) |> 
  mutate(percent = round(n/nrow(df)*100)) |> 
  mutate(N_percent = paste0(n,"/", nrow(df), " (",percent,"%)"), .keep = "unused") |> 
  select(label = x, stat_0 = N_percent) |> 
  mutate(grouping_var = "x", .before = 1L) |> 
  # create a gtsummary class listing
  tbl_listing(df, group_by = grouping_var) |> 
  # style table with gtusmmary functions to make it look like `tbl_summary()`
  modify_header(
    label ~ "**Characteristic**",
    stat_0 ~ "**N = 5**"
  ) |> 
  modify_column_alignment(stat_0, align = "center") |> 
  modify_footnote(stat_0 = "n/N (%)")

tbl1 <- df1 |> 
  select(y) |> 
  tbl_summary(
    type = list(y ~ "categorical"))

tbl_stack(
  tbls = list(tbl, tbl1)
)


x1c 0d1x的数据

f3temu5u

f3temu5u2#

在我的answer的基础上,你可以使用tbl_custom_summary来实现你想要的结果。这里是使用我的自定义函数的一个通用版本,它也允许传递标识符列的名称,并通过添加distinct参数来处理这两种类型的变量。

library(dplyr, warn = FALSE)
library(tidyr)
library(gtsummary)

df <- df |>
  left_join(df1, by = "id")

my_summary <- function(variable, .id = "id", distinct = FALSE) {
  function(data, full_data, stat_display, ...) {
    if (distinct) {
      data <- distinct(data, .data[[.id]], .data[[variable]])
    }
    data |>
      summarise(
        n = n(),
        N = n_distinct(full_data[[.id]]),
        p = n / N
      )
  }
}

tbl <- df %>%
  separate_rows(x, sep = ",") |>
  tbl_custom_summary(
    include = -id,
    stat_fns = list(
      ~ my_summary("x", "id"),
      ~ my_summary("y", "id", distinct = TRUE)
    ),
    type = everything() ~ "categorical",
    statistic = list(
      x ~ "{n}/{N} ({p}%)",
      y ~ "{n} ({p}%)"
    )
  )

tbl |>
  modify_header(
    stat_0 ~ paste0("**N = ", nrow(df), "**")
  ) |> 
  modify_footnote(stat_0 = "n/N (%)")

字符串


的数据

相关问题