I am trying to create a expss like cross table for multiple variables . i have a database which already labelled.
df <- data.frame("TB1"=c("OPS", "OPS", "HR", "ADMIN", "HR", "ADMIN", "ADMIN", "HR", "HR", "HR", "Sales", "Sales", "Sales", "HR", "HR", "HR", "HR", "Sales", "Sales"),
"TB2"=c("Sales", "ADMIN", "ADMIN", "Sales", "ADMIN", "ADMIN", "ADMIN", "HR", "HR", "HR", "HR", "HR", "HR", "OPS", "OPS", "OPS", "OPS", "HR", "HR"),
"TB3"=c("ADMIN", "Sales", "OPS", "Sales", "HR", "ADMIN", "HR", "HR", "ADMIN", "ADMIN", "HR", "HR", "HR", "OPS", "HR", "OPS", "HR", "HR", "Sales"),
"TB4"=c("Global", "Regional", "Regional", "Global", "Global", "Regional", "Regional", "Global", "Global", "Regional", "Regional", "Global", "Global", "Regional", "Global", "Regional", "Global", "Regional", "Global"))
banner <- with(df, list(total(),TB4))
t1 <- df %>% tab_cells(mdset(TB1 %to% TB3)) %>%
tab_cols(total(), banner) %>%
cross_cpct() %>%
tab_pivot()
the table should be look like below
Here is a suggestion! I am not quite sure about the percent. But using gtsummary
with tbl_strata
function and tbl_cross
function could be a good start for you: https://www.danieldsjoberg.com/gtsummary/reference/tbl_strata.html https://www.danieldsjoberg.com/gtsummary/reference/tbl_cross.html
library(dplyr)
library(tidyr)
library(gtsummary)
df %>%
pivot_longer(
-TB4
) %>%
tbl_strata(
strata = TB4,
.tbl_fun =
~ .x %>%
tbl_cross(
row = value,
col = name,
percent = "row",
margin="row")
)
Something like this:
library(expss)
df <- data.frame("TB1"=c("OPS", "OPS", "HR", "ADMIN", "HR", "ADMIN", "ADMIN", "HR", "HR", "HR", "Sales", "Sales", "Sales", "HR", "HR", "HR", "HR", "Sales", "Sales"),
"TB2"=c("Sales", "ADMIN", "ADMIN", "Sales", "ADMIN", "ADMIN", "ADMIN", "HR", "HR", "HR", "HR", "HR", "HR", "OPS", "OPS", "OPS", "OPS", "HR", "HR"),
"TB3"=c("ADMIN", "Sales", "OPS", "Sales", "HR", "ADMIN", "HR", "HR", "ADMIN", "ADMIN", "HR", "HR", "HR", "OPS", "HR", "OPS", "HR", "HR", "Sales"),
"TB4"=c("Global", "Regional", "Regional", "Global", "Global", "Regional", "Regional", "Global", "Global", "Regional", "Regional", "Global", "Global", "Regional", "Global", "Regional", "Global", "Regional", "Global"))
df %>%
to_long(keep = TB4) %>%
tab_cols(list(total(), TB4) %nest% variable) %>%
tab_cells("|" = value) %>%
tab_stat_cpct() %>%
tab_pivot()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.