function to getting results of from table

I am trying to create a function from which i can get sum of top two variables from the table of output of ratios.

library(expss)

data<-data.frame(
  gender = c(1,2,1,2,1,2,1,2,2,2,2,1,1,2,2,2,2,1,1,1,1,1,2,1,2,1,2,2,2,1,2,1,2,1,2,1,2,2,2),
  sector = c(3,3,1,2,5,4,4,4,4,3,3,4,3,4,2,1,4,2,3,4,4,4,3,1,2,1,5,5,4,3,1,4,5,2,3,4,5,1,4),
  col1=c(1,1,2,0,2,0,0,2,1,0,0,2,0,3,0,3,0,1,0,3,0,1,1,2,0,1,1,3,0,3,0,1,2,0,3,0,1,0,1),
  col2=c(1,1,1,1,1,0,3,3,2,1,1,1,2,1,0,2,0,1,2,1,0,1,2,1,1,1,0,2,0,1,1,2,1,1,1,1,2,0,0),
  col3=c(1,1,0,0,0,0,2,1,3,2,0,3,0,2,0,2,1,0,2,0,2,0,1,3,1,0,0,0,1,0,3,1,1,1,1,1,3,0,1),
  col4=c(1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  col5=c(1,2,1,1,1,2,1,2,2,1,2,1,1,1,2,2,2,1,1,1,2,1,2,1,1,1,2,2,2,1,1,2,2,1,1,1,2,2,2)
)

data$col1<-factor(data$col1, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col2<-factor(data$col2, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col3<-factor(data$col3, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col4<-factor(data$col4, levels=c(1,0), labels=c("USA","CA"))
data$col5<-factor(data$col5, levels=c(1,0), labels=c("Local","Regional"))

data$gender<-factor(data$gender, levels=c(1,2), labels=c("Male","female"))
data$sector<-factor(data$sector, levels=c(1,2,3,4,5), labels=c("TX","CA","NY","LA","WA"))


data$gender1 <- ifelse(data$gender == "Male",1, NA)
data$total <- ifelse(data$col5 == "Local",1, NA)

val_lab(data$gender1)<-c("GENDER"=1)
val_lab(data$total)<-c("All Market"=1)

lkl <- with(data,list(total,gender1))


cro(data$col2)
cro(data$col2)
cro(data$col3)


Tb <- data %>%
  tab_cells(mrset(col1 %to% col3)) %>%
  tab_cols(lkl) %>%
  tab_stat_cpct() %>% 
  tab_pivot()

for example from below table i want to show sum of top two from below tables like

the final output should look like (numbers of gender are hard coded)

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.