'tab_stat_cpct not giving the required result

I am trying to create a table as shown in picture for multi response data with list of banner . i have tried the below way but didn't worked.

library(expss)
mtcars1 <- mtcars
mtcars1$dd <- ifelse(mtcars1$gear == 4,1,NA)
mtcars1$ff <- ifelse(mtcars1$gear == 5,1,NA)

mtcars1$dd<-factor(mtcars1$dd, levels=c(1), labels=c("Local"))
mtcars1$ff<-factor(mtcars1$ff, levels=c(1), labels=c("Regional"))

mtcars1$vs<-factor(mtcars1$vs, levels=c(0,1), labels=c("Male","female"))
mtcars1$am<-factor(mtcars1$am, levels=c(0,1), labels=c("Male","female"))

mk <- with(mtcars1,list(total(),dd,ff))

T1 <- mtcars1 %>%
  tab_cells(vs,am) %>%
  tab_cols(mk) %>%
  tab_stat_cpct() %>% 
  tab_pivot() %>%
  split_table_to_df()

required out look like enter image description here

r


Solution 1:[1]

If you use dichotomy encoding, you need to set variable labels rather than value labels. And you should to specify columns from which consists mutiple response set. For dichotomy, it is mdset function:

library(expss)
data(mtcars)
mtcars1 <- mtcars
mtcars1$dd <- ifelse(mtcars1$gear == 4,1,NA)
mtcars1$ff <- ifelse(mtcars1$gear == 5,1,NA)

mtcars1$dd<-set_var_lab(mtcars1$dd, "Local")
mtcars1$ff<-set_var_lab(mtcars1$ff, "Regional")

mtcars1$vs<-factor(mtcars1$vs, levels=c(0,1), labels=c("Male","female"))
mtcars1$am<-factor(mtcars1$am, levels=c(0,1), labels=c("Male","female"))

mk <- with(mtcars1,list(total(),mdset(dd, ff)))

T1 <- mtcars1 %>%
    tab_cells(vs,am) %>%
    tab_cols(mk) %>%
    tab_stat_cpct() %>% 
    tab_pivot() %>%
    split_table_to_df() 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gregory Demin