问题描述
所以我对数据框进行了子集化以仅保留我感兴趣的 4 列。我想计算控制 (0) 和处理 (1) 观察的数量。我用 gtsummary 包计算了一些东西,但是变量是垂直方向的(就像这里的 http://www.danieldsjoberg.com/gtsummary/articles/tbl_summary.html),一个在一个下方,这不是我想要的。我在谷歌上搜索过,但我看到的所有表格也都有这个方向。
如果你们中的一些人有任何想法,我在这里放了一张我想获得的图片!
install.packages("gtsummary")
library(gtsummary)
trial <- finaldf %>% select(treatment,2digID,4digID,classificationsdescription)
trial %>% tbl_summary()
t2 <- trial %>% tbl_summary(by = treatment)
我无法放置真实数据,但我创建了一个看起来像我的数据的示例:
_2ID <- c(38,38,80,80)
_4ID <- c(3837,3837,3812,3896,3894,3877,8099,8027,8033,8064,8022)
descriptions <- c('ILL1','ILL1','ILL2','ILL3','ILL4','ILL5','ILL5')
trt <-c(0,1,0)
df.data <- data.frame(_2ID,_4ID,descriptions,trt)
更新 - 已解决
我想我设法解决了这个问题,即使我的输出是一个数据框而不是一个“发布就绪”表:
install.packages("reshape2")
library(reshape2)
data_wide <- dcast(df,_2digID+_4digID+descriptions ~ treatment,value.var="counts")
但我还不确定这是否给出了正确的数字。
解决方法
下面的例子让你关闭,但不是你想要的。我喜欢能够支持这样的表格的想法,我会将它添加到要实现的功能列表中!
library(gtsummary)
#> #Uighur
packageVersion("gtsummary")
#> [1] '1.4.1'
tbl <-
trial %>%
mutate(
grade = paste("Grade",as.character(grade)),stage = paste("Stage",as.character(stage))
) %>%
tbl_strata(
strata = c(stage,grade),~ .x %>%
tbl_summary(by = trt,include = response,type = response ~ "categorical",missing = "no",statistic = response ~ "{n}") %>%
modify_header(all_stat_cols() ~ "**{level}**"),.combine_with = "tbl_stack"
) %>%
as_flex_table()
- 表格被截断,因为它很长!
由 reprex package (v2.0.0) 于 2021 年 7 月 14 日创建