具有三个分类变量的 Rstudio gtsummary 表

发布于 2025-01-10 22:36:46 字数 1322 浏览 0 评论 0原文

我正在尝试弄清楚如何将 gtsummary 包用于我的数据集。

我有三个分类值,其中两个设置为分层。我对每个样本的频率不感兴趣,但想要表中的数值。

目前我正在使用这个简单的代码(x、y、z 是我的分类值,而 SOC 是数值。Y 和 Z 应该放在标题(地层)中。

Data %>%
  select(x, y, z , SOC) %>%
  tbl_strata(strata=z,
             .tbl_fun =
               ~ .x %>%
               tbl_summary(by = y , missing = "no"),
              statistic = list(all_continuous()~ "{mean} ({sd})" ))%>%
  modify_caption("**Soil organic carbon [%]**")%>%
  bold_labels()

编辑

让我们以试验数据集为例:

trial %>%
  select(trt, grade, stage, age, marker) %>%
  tbl_strata(strata=stage,
             ~tbl_summary(.x, by = grade , missing = "no"),
             statistic = list(all_continuous()~ "{mean} ({sd})" 
))%>%
  bold_labels()

什么我正在寻找这样的表格,但没有显示每种治疗的频率(药物 A、B),我只希望年龄和标记显示在我的表格中,但按我想要的治疗进行组织。第一部分仅显示年龄和标记对于接受药物 A 的组。然后是显示药物 B 相同情况的部分。

在此处输入图像描述

编辑 2

您的输入正是我正在寻找的内容。使用试​​用数据集,它工作得很好。但是,我输入我的数据,数值全部在一列而不是行中。我仍然得到频率,但我不明白为什么我使用完全相同的代码和相同数量的变量,我的表格看起来有点像这样:

在此处输入图像描述< /a>

I'm trying to figure out how to use the gtsummary package for my dataset.

I have three categorical values and two of those set as strata. I'm not interested in the frequency of each sample but want the numeric value in the table.

Currently I'm using this simple code (x, y, z, are my categorical values, whereas SOC is the numerical values. Y and Z should go in the headline (strata).

Data %>%
  select(x, y, z , SOC) %>%
  tbl_strata(strata=z,
             .tbl_fun =
               ~ .x %>%
               tbl_summary(by = y , missing = "no"),
              statistic = list(all_continuous()~ "{mean} ({sd})" ))%>%
  modify_caption("**Soil organic carbon [%]**")%>%
  bold_labels()

Edit

Let's take the trial dataset as an example:

trial %>%
  select(trt, grade, stage, age, marker) %>%
  tbl_strata(strata=stage,
             ~tbl_summary(.x, by = grade , missing = "no"),
             statistic = list(all_continuous()~ "{mean} ({sd})" 
))%>%
  bold_labels()

What I'm looking for is a table like this, but without the frequency showing of each treatment (Drug A, B). I only want the age and marker to show up in my table but organized by treatment. I'd like to have the first section showing only the age and marker for the group that received Drug A. Then a section showing the same for Drug B.

enter image description here

Edit 2

Your input is exactly what I am looking for. With the trial dataset it works perfectly fine. However, ones I put in my data, the numeric values are all in one column instead of in rows. I also still get the frequencies and I can't figure out why. I use exactly the same code and the same amount of variables and my table looks somewhat like this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2025-01-17 22:36:46

我认为对 tbl_strata() 的嵌套调用(一个合并,另一个堆叠)将为您带来所需的结果。下面举例!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'

tbl <- 
  trial %>%
  select(trt, grade, stage, age, marker) %>%
  tbl_strata(
    strata = trt, 
    function(data) {
      data %>%
        tbl_strata(
          strata = stage,
          ~ tbl_summary(
            .x,
            by = grade,
            statistic = all_continuous() ~ "{mean} ({sd})",
            missing = "no"
          ) %>%
            modify_header(all_stat_cols() ~ "**{level}**")
        )
    },
    .combine_with = "tbl_stack",
    .combine_args = list(group_header = c("Drug A", "Drug B"))
  ) %>% 
  bold_labels()

输入图片此处描述
reprex 包 (v2.0.1) 创建于 2022 年 3 月 4 日

I think nesting calls to tbl_strata() (one merging and the other stacking) will get you what you're after. Example below!

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'

tbl <- 
  trial %>%
  select(trt, grade, stage, age, marker) %>%
  tbl_strata(
    strata = trt, 
    function(data) {
      data %>%
        tbl_strata(
          strata = stage,
          ~ tbl_summary(
            .x,
            by = grade,
            statistic = all_continuous() ~ "{mean} ({sd})",
            missing = "no"
          ) %>%
            modify_header(all_stat_cols() ~ "**{level}**")
        )
    },
    .combine_with = "tbl_stack",
    .combine_args = list(group_header = c("Drug A", "Drug B"))
  ) %>% 
  bold_labels()

enter image description here
Created on 2022-03-04 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文