具有计算数据集/加权计数的 gtsummary 中的交叉表

问题描述

我需要更多帮助。我需要一个最可发表的格式(科学论文)的交叉表。对于这些需求,我一直在使用 gtsummary。

我拥有的数据框是其他描述性例程完成的先前计数的结果。

enter image description here

我尝试过使用 tbl_summary、tbl_cross、tbl_strata。但是,最接近我需要的那个返回了行数的描述性计算和统计信息,而不是数据帧的计算(变量 n)。

enter image description here

抱歉,我尝试了所有方法。我什至将 df 转换为宽格式。但无论如何,我缺乏这样做的经验和知识!

# Data frame
Result <- as.factor(c("Derrota","Empate","Vitória","Derrota","Vitória"))

Context <- as.factor(c("Overall match","Overall match","  
Overall match","First half","After balanced halftime","After balanced halftime"))

n <- as.numeric(c(583,607,1217,487,1100,820,178,304,367))

df <- data.frame(Result,Context,n)

# Definitive table of results
library(gtsummary) # presentation-ready data summary and analytic result tables
df %>%
  select(c('Result','n','Context')) %>% 
     tbl_summary(by=Result)

解决方法

这是您要找的吗?

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

df <- 
  data.frame(
    Result = c("Derrota","Empate","Vitória","Derrota","Vitória"),Context = c("Overall match","Overall match","First half","After balanced halftime","After balanced halftime"),n = c(583,607,1217,487,1100,820,178,304,367)
  )

tbl <-
  # convert data frame to survey object that accounts for the weights
  survey::svydesign(~1,data = df,weights = ~n) %>%
  # summarize weighted data
  tbl_svysummary(by = Result,percent = "cell")

enter image description here reprex package (v1.0.0) 于 2021 年 2 月 27 日创建