1

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

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

我想通过将 Home Visits 和 Enrollment Duration 的平均统计数据作为列来改进表格设计,从而为每个 Marital Status 级别(以及 中的其他变量tab_rows)保存一行。这是如何实现的?另外,是否可以对交替行进行遮蔽?

表达式表

4

1 回答 1

1

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

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

在此处输入图像描述

于 2020-01-10T23:07:42.330 回答