具有三个分类变量的 Rstudio gtsummary 表
我正在尝试弄清楚如何将 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
您的输入正是我正在寻找的内容。使用试用数据集,它工作得很好。但是,我输入我的数据,数值全部在一列而不是行中。我仍然得到频率,但我不明白为什么我使用完全相同的代码和相同数量的变量,我的表格看起来有点像这样:
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.
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为对
tbl_strata()
的嵌套调用(一个合并,另一个堆叠)将为您带来所需的结果。下面举例!由 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!Created on 2022-03-04 by the reprex package (v2.0.1)