'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 this 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(varlist,1)
  col2<-tail(varlist,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
}

debug(fun1)
fun1(dataset=df,varlist=c("TB1","TB2","TB3"),banner=banner1)

#error
Error in data.table::melt.data.table(data = data.table::as.data.table(data),  : 
  Unknown 'id.vars' type list, must be character or integer vector

expected output: enter image description here

r


Solution 1:[1]

In your case it is better to avoid long form and use loop:

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))



fun1<- function(dataset,varlist,banner){
    intermediate_table = dataset %>% 
        tab_cols(banner)
    for(each_var in varlist){
        intermediate_table = intermediate_table %>% 
            tab_cells("|" = get(each_var)) %>% 
            tab_stat_cpct(label = each_var)
    }
    intermediate_table %>% 
        tab_pivot(stat_position = "inside_columns")
}

fun1(dataset=df, varlist=c("TB1","TB2","TB3"),banner=banner1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gregory Demin