连续使用多个变量旋转 table

Pivoting the table with multiple variables in a row

我正在使用 R 中的 expss 包来创建 tables。抱歉,我无法理解文档。

这是我的示例数据:

dput(df)
structure(list(cohort_tracing_complete = structure(c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
0, 0, 0, 0, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2), label = "Form Complete", class = c("labelled", 
"numeric"), labels = c(Incomplete = 0, Unverified = 1, Complete = 2
)), crf_1_eligibility_consent_recruitment_complete = structure(c(2, 
2, 2, 2, 2, 2, 2, 0, 0, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), label = "Form Complete", class = c("labelled", 
"numeric"), labels = c(Incomplete = 0, Unverified = 1, Complete = 2
))), row.names = c(NA, -40L), class = c("tbl_df", "tbl", "data.frame"
))

df %>% 
  tab_cells(
    cohort_tracing_complete,
    crf_1_eligibility_consent_recruitment_complete) %>%
  tab_cols(total()) %>%
  tab_stat_cases(
    total_label = NULL,
    total_statistic = "u_cases",
    total_row_position = "below",
  ) %>%
  tab_pivot() %>%
  tab_transpose

我正在尝试让我的数据看起来像这样

所需格式

我尝试使用上面的代码转换我的 table,现在它看起来像这样

所有数字都在一行中。

如何调整我的代码才能获得所需的格式?

我知道这是使用 dplyrtidyr 而不是 expss 实现的。

我快速浏览了一下 expss 所有示例似乎都是关于在至少两个变量之间对汇总数据进行子集化而不是两个变量的汇总数据。

library(tidyr)
library(dplyr)

  df %>% 
  pivot_longer(everything()) %>% 
  group_by(name, value) %>% 
  summarise(count = n()) %>%
  pivot_wider(names_from = value, values_from = count)%>% 
  rowwise() %>% 
  mutate(Total = sum(c(Incomplete, Unverified, Complete), na.rm = TRUE))

# A tibble: 2 x 5

  name                                           Incomplete Unverified Complete Total
  <chr>                                               <int>      <int>    <int> <int>
1 cohort_tracing_complete                                28          1       11    40
2 crf_1_eligibility_consent_recruitment_complete         25         NA       15    40