我有一项调查需要过滤以仅包含两个特定类别以进行比较。可以使用以下代码重现它的模拟:
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?
我在数据框中使用过滤器,但我担心它会导致我失去举重
使用
droplevels()
功能:
svy$variables$stype %<>% droplevels