如何在 R 中对调查对象进行子集化并使用 tbl_svysummary 和 add_difference 函数

问题描述 投票:0回答:1

我有一项调查需要过滤以仅包含两个特定类别以进行比较。可以使用以下代码重现它的模拟:

svy <- survey::svydesign(id = ~dnum, weights = ~pw, data = apiclus1, fpc = ~fpc) 

svy <- subset(svy, stype %in% c("E", "H"))

我正在尝试使用此代码创建一个具有差异的表:

svy %>%
  tbl_svysummary(by = stype, include = c(api00, stype),
                 statistic = list(all_categorical() ~ "{p}%",
                     all_continuous() ~ "{mean}"),
    digits = ~ 2,
    missing = "no") %>% 
    add_difference()

并收到此错误:错误:“tbl_summary”/“tbl_svysummary”对象必须具有恰好两个级别的

by=
值。

如何将级别降至 2,从而使用 add_difference?

我在数据框中使用过滤器,但我担心它会导致我失去举重

r survey gtsummary
1个回答
0
投票

使用

droplevels()
功能:

svy$variables$stype %<>% droplevels
© www.soinside.com 2019 - 2024. All rights reserved.