I want to create a function with the summary i am creating with the code below.
i tried but it doesn't work for me .
library(expss)
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"))
banner1 <- with(df, list(total(),TB4))
#this is working fine but i want a function
df %>%
to_long(keep = TB4) %>%
tab_cols(list(total(), TB4) %nest% variable) %>%
tab_cells("|" = value) %>%
tab_stat_cpct() %>%
tab_pivot()
# objective was to create a function like fun(dataset=df, varlist=c("TB1","TB2","TB3"),banner=banner1)
#and i tired like below
fun1<- function(dataset,varlist,banner){
data<-dataset[[varlist]]
col1<- head(var_list,1)
col2<-tail(var_list,1)
var_lab(colnames(dataset)[ncol(dataset)]) <- ""
t1<- data %>%
to_long(keep = Banner) %>%
tab_cols(col1 %nest% col2) %>%
tab_cells("|" = value) %>%
tab_stat_cpct() %>%
tab_pivot()
t1
}