跳过 expss 表中二分变量的“零”级
Posted
技术标签:
【中文标题】跳过 expss 表中二分变量的“零”级【英文标题】:Skip "zero" level of dichotomous variables in expss tables 【发布时间】:2020-01-03 19:30:19 【问题描述】:我想使用 expss 包为一些二分变量创建一个汇总表。由于变量是二分的,因此两个级别之一就足以“显示图片”。
我尝试使用函数 tab_net_cell,但无法获得正确的结果。下面是一些带有 1 或 0 的 BrCa(乳腺癌)的示例代码。我只想显示患有但不患有乳腺癌的患者数量。
df <- data.frame(BrCa = c(1,1,1,0,0,0,NA,NA,0,0))
df$group <- c(1,2,1,2,1,2,1,2,1,2)
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa) %>%
expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows")
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa) %>%
expss::tab_net_cells("BrCa" = eq(1)) %>%
expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows")
【问题讨论】:
【参考方案1】:最简单的方法是过滤结果表:
df <- data.frame(BrCa = c(1,1,1,0,0,0,NA,NA,0,0))
df$group <- c(1,2,1,2,1,2,1,2,1,2)
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa) %>%
expss::tab_stat_cpct(total_row_position = "none",label = "%") %>%
expss::tab_stat_cases(total_row_position = "none",label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows") %>%
expss::where(grepl(1, row_labels))
另一种方法是使用均值和总和而不是cpct和cases:
df %>%
expss::tab_cols(group) %>%
expss::tab_cells(BrCa*100) %>%
expss::tab_stat_mean(label = "%") %>%
expss::tab_stat_sum(label = "N") %>%
expss::tab_pivot(stat_position = "inside_rows")
【讨论】:
感谢平均值和总和的提示。现在,这是我的解决方案df %>% expss::tab_cols(group) %>% expss::tab_cells(BrCa) %>% expss::tab_stat_fun("%" = function (x) mean(x,na.rm = T)*100, "N" = function (x) sum(x,na.rm = T)) %>% expss::tab_pivot(stat_position = "inside_rows")
能否请您添加过滤解决方案?
@SilkeZachariae 抱歉,我将相同的代码粘贴了两次。查看第一段代码的更新。以上是关于跳过 expss 表中二分变量的“零”级的主要内容,如果未能解决你的问题,请参考以下文章