如何进一步细化 expss table 格式?

how to further refine expss table format?

我正在尝试使用 expss 改进我的 table 设计。我当前的设计如下所示,使用以下代码:

library(expss)
# bogus example data
x<-structure(list(visits= structure(c(17, 2, 23, 1, 21), label = "Total # Home Visits", class = c("labelled", "numeric")), months_enrolled = structure(c(21.42474, 51.105, 52.474, 53.75, 60.0392105), label = "Enrollment Duration (months)", class =c("labelled","numeric")), marital2 = structure(c("Married", NA, "Married", "Married", "Married"), label = "Marital Status", class = c("labelled", "character")), Relationship2 = structure(c("Mother", "Mother", "Mother", "Mother", "Mother"), label = "Relationship (recoded)", class = c("labelled", "character"))), row.names = c(NA, 5L), class = "data.frame")

htmlTable(x %>% 
tab_cells(visits,months_enrolled) %>%
tab_rows(marital2, Relationship2,  total()) %>%     tab_stat_fun(Mean = w_mean, "Valid N" = w_n, method = list) %>%
tab_pivot() %>%
set_caption("Table 6: Bogus Visits and Duration by Characteristics") %>% 
htmlTable(.,css.cell = c("width: 220px", # first column width
                          rep("width: 50px", ncol(.) - 1))))

我想改进 table 设计,将家访和注册持续时间的平均统计数据作为列,从而为婚姻状况的每个级别(以及 [=13 中的其他变量)保存一行=]).这是如何实现的?另外,是否可以遮挡交替行?

看来,最简单的方法就是转置table:

htmlTable(x %>% 
              tab_cells(visits, months_enrolled) %>%
              tab_cols(marital2, Relationship2,  total()) %>%  
              tab_rows(total(label = "|")) %>% 
              tab_stat_fun(Mean = w_mean, "Valid N" = w_n) %>%
              tab_pivot() %>%
              tab_transpose() %>% 
              set_caption("Table 6: Bogus Visits and Duration by Characteristics") %>% 
              htmlTable(.,css.cell = c("width: 220px", # first column width
                                       rep("width: 50px", ncol(.) - 1))))