creating function to obtain result of table

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
}

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.