I want to display only two rows and last row and want to create a function which have input paramter to show top rows and bottom rows.
now the output of T2 is looking like below
but the required output should be look like below with some input parameter which is required to show how much top records to be shown and how much bottom records to be shown.
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),
col1=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),
col2=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),
col3=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),
col4=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),
col5=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$col1<-factor(data$col1, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col2<-factor(data$col2, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col3<-factor(data$col3, levels=c(1,2,3,0), labels=c("sale","Ops","MGMT","Mark"))
data$col4<-factor(data$col4, levels=c(1,0), labels=c("USA","CA"))
data$col5<-factor(data$col5, 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$col5 == "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)$")
T1 <- dataset %>%
tab_cells(mrset_p(npatt)) %>%
tab_cols(banner) %>%
tab_stat_cpct() %>%
tab_pivot()
T1
}
debug(fun1)
t1 <- fun1(data, "col_",lkl)
T2 <- data %>%
tab_cells(mrset(col.1 %to% col.3))%>%
tab_cols(lkl) %>%
tab_stat_cpct() %>%
tab_pivot()
#tried this too but didn't worked
T4 <- data %>%
tab_cells(mrset(col.1 %to% col.3), "Bottom 2 Box" = nrow(1:2), "Top 2 Box" = 4, position = "bottom") %>%
tab_cols(total(), lkl) %>%
tab_stat_cpct() %>%
tab_pivot()