在 gtsummary 中交叉 table 计算数据 set/weighted 计数

Cross table in gtsummary with computed data set/weighted count

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

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

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

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

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

Context <- as.factor(c("Overall match", "Overall match", "  
Overall match", "First half", "First half", "First half",   
"After balanced halftime", "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", "Empate", "Vitória",
               "Derrota", "Empate", "Vitória"), 
    Context = c("Overall match", "Overall match", 
                "Overall match", "First half", "First half", "First half",   
                "After balanced halftime", "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")

reprex package (v1.0.0)

于 2021-02-27 创建