Crosstab function for multi-response function

I have a function for Single select question to reflect cross-tab summary. how i can create a function like this for multi response questions table, do we have any easy solution for this ...???

dataa<-data.frame(
  aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
  col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,2,4,4,4,1,2,5,3,5),
  col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,3,2,7,7,2,3,7,2,4),
  col2=c(5,1,5,7,4,4,2,4,5,4,1,6,1,3,7,2,3,4,1,3,1,2,3,6,5,7,6,5,3,7,7,3,2,3,7,1,3,6,1,3)
)

masking_criteria<-c(3,4,5)
mask_m<-function(x,N){
  x= ifelse(N<masking_criteria[1],"--",x)
}

group_var="Variable name for Cut"
tab_std_cross1<-function(data, var, group_var) { 
  T1<- as.data.frame.matrix(table(data[[var]],data[[group_var]]))
  overall=rowSums(T1)
  T1<-cbind("Worldwide"=overall,T1)
  N=as.data.frame(colSums(T1))
  for(i in 1:ncol(T1)) {
    T1[,i]=round((T1[,i]*100)/N[i,1],digits = 0)
  }
  for(i in 1:ncol(T1)) {
    T1[,i]<-sapply(T1[,i], function(x) ifelse(mask_m(x,N[i,1])=="--","--",paste0(mask_m(x,N[i,1]),"%")))  
  } 
T1<-rbind("N"=N[,1],T1)  
  T1<-as.data.frame(T1)
  T3<- rownames(T1)
  T3=as.data.frame(T3)
  colnames(T3)=" "
  T2<-cbind(T3,T1)  
  T2%>% flextable()
  
  }

tab_std_cross1(data=dataa, var = "col1", group_var = "aa") 

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.