1

我想使用 expss 包为一些二分变量创建一个汇总表。由于变量是二分的,因此两个级别之一就足以“显示图片”。

我尝试使用函数 tab_net_cell,但无法获得正确的结果。这是一些示例代码,其中 BrCa(乳腺癌)为 1 或 0。我只想显示患有但不患有乳腺癌的患者数量。

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") 

4

1 回答 1

2

最简单的方法是过滤结果表:

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))

另一种方法是使用 mean 和 sum 而不是 cpct 和 case:

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") 
于 2019-08-30T15:18:52.690 回答