selecting column with grepl in custom function

I have a custom function like below in which i use to give parameter as first column and last column but now i am looking for a solution where i doesn't need to give first column and last column and it will automatically detect first column dynamically and last column from data frame and create a summary.

  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.2.1=c(1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,0,0,0,1,0,0,0,0,1,0,1),
    col1.2.2=c(1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0),
    col1.2.3=c(1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0,1,1,1,1,1,0,0,1),
    col1.3.1=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),
    col1.3.2=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$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$col1.2.1<-factor(data$col1.2.1, levels=1, labels="Sales")
  data$col1.2.2<-factor(data$col1.2.2, levels=1, labels="OPS")
  data$col1.2.3<-factor(data$col1.2.3, levels=1, labels="Management")
  data$col1.3.1<-factor(data$col1.3.1, levels=1, labels="HR")
  data$col1.3.2<-factor(data$col1.3.2, levels=c(1,2), labels=c("National","Overseas"))
  
  library(expss)
  
  

tab_text_multi_sort <- function(data,var_list,first_col_param,second_col_param) {  
  data1<-data[var_list] %>% as.data.frame()
  var_lab(colnames(data1)[ncol(data1)]) <- ""
  tab1 <- eval(parse_expr(paste0("tab_cells(data1,mdset(",
                                 first_col_param ," %to% ",second_col_param,"))"))) %>% 
    tab_cols(total(),data1[1]) %>% 
    tab_stat_cpct() %>% tab_pivot()
  tab1 <-  as.data.frame(tab1)
}

T1 <- tab_text_multi_sort (data = data,var_list = c("col1.2.1","col1.2.2","col1.2.3"),first_col_param = "col1.2.1",second_col_param ="col1.2.3") 

it can automatically detect first column and last column from var_list list like grepl or something else. do we have any solution like this ...??


tab_text_multi_sort <- function(data,var_list) {  
  first_col_param <- head(var_list,1)
  second_col_param <- tail(var_list,1)

  data1<-data[var_list] %>% as.data.frame()
  var_lab(colnames(data1)[ncol(data1)]) <- ""
  tab1 <- eval(parse_expr(paste0("tab_cells(data1,mdset(",
                                 first_col_param ," %to% ",second_col_param,"))"))) %>% 
    tab_cols(total(),data1[1]) %>% 
    tab_stat_cpct() %>% tab_pivot()
  tab1 <-  as.data.frame(tab1)
}

T1 <- tab_text_multi_sort (data = data,
                          var_list = c("col1.2.1",
                                       "col1.2.2",
                                       "col1.2.3"))

This topic was automatically closed 7 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.