I am trying to give column names as regular expression for input parameter to function but its not working, may be i am doing something wrong.
- i want to give column names as regular expression (col_) previously i want giving a vector of variables (col_1,col_2,col_3)
- i want to as varlab in the table output like below (yellow)
tried giving lab as input parameter and label = lab in function but not working
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),
col_1=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),
col_2=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),
col_3=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),
coll.4=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),
coll.5=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$col_1<-factor(data$col_1, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col_2<-factor(data$col_2, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col_3<-factor(data$col_3, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$coll.4<-factor(data$coll.4, levels=c(1,0), labels=c("USA","CA"))
data$coll.5<-factor(data$coll.5, 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$coll.5 == "Local",1, NA)
val_lab(data$gender1)<-c("GENDER"=1)
val_lab(data$total)<-c("All Market"=1)
lkl <- with(data,list(total,gender1))
fun1<- function(dataset,pattern,banner){
print(pattern)
npatt<-paste0(pattern, ".*(?<!_TEXT)$")
lab<-paste0(pattern, "[fun1]:", label)
intermediate_table = dataset %>%
tab_cols(banner)
for(each_var in pattern){
intermediate_table = intermediate_table %>%
tab_cells("|" = get(mrset_p(each_var))) %>%
tab_stat_cpct(label = each_var)
}
intermediate_table %>%
tab_pivot(stat_position = "inside_columns")
}
debugonce(fun1)
t1 <- fun1(dataset=mtcars1, pattern="col_",banner=mk,"Table 1")