table for multiple variables with multiple labels

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
image

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.