I am trying to create a function to get the below required output. i am looking for a function which i can iterate to the list of data frames.
below function i have tried to create but unable to bind the tables for list of variables.
also i am unable to apply banner here as required.
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 <- list(data$gender1,data$total)
lkl <- with(data,list(data$gender1,data$total))
Fun1 <- function(df,var_l,var_name_list,Banner){
Fun2 <- function(df,var,var_name,Banner){
expss::var_lab(df[[var]]) <- ""
t1 <- expss::cro_cpct(df[var]) %>% t()
t1[,1] <- var_name[i]
t1
}
freq_list<-list()
for(i in 1:length(var_l)){
freq_list[i] <-Fun2(df,var_l[i],var_name_list[i])
t2<- do.call(rbind,freq_list)
}
t2
}
debug(Fun1)
t1 <- Fun1(df=data,var_l = c("col1","col2","col2"),var_name_list = c("aa","bb","cc","dd"),lkl)
the required output should look like . i am looking for solution in expss because i am looking for spss like tables output.
Gender | All Market | |||||||
---|---|---|---|---|---|---|---|---|
Sale | Ops | Mgmt | Mark | Sale | Ops | Mgmt | Mark | |
AA | 33.3 | 15.3 | 12.8 | 38.2 | 33.3 | 15.3 | 12.8 | 38.2 |
BB | 33.3 | 15.3 | 12.8 | 38.2 | 33.3 | 15.3 | 12.8 | 38.2 |
CC | 33.3 | 15.3 | 12.8 | 38.2 | 33.3 | 15.3 | 12.8 | 38.2 |
Total_cases | 99 | 42 | 63 | 21 | 32 | 33 | 16 | 32 |