select few rows to display from output of table

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

image

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.

image

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




If I understood your problem correctly something like this should do the trick:

missingMiddle <- function(x, keepTop, keepBottom) {
  top <- head(x, keepTop)
  bottom <- tail(x, keepBottom)
  mm <- rbind(top,bottom)
  return(mm)
}

head() keeps the top n rows; tail() keeps the bottom n rows. You can pass arguments to them in your function so you can select the number of rows. Then rbind the 'top' and 'bottom' together and you have what (I think) you're looking for.

Example:

bob <- tibble(
  n = 1:20,
  v = runif(20, 0, 100))


missingMiddle(bob, 1,2)
# A tibble: 3 x 2
      n     v
  <int> <dbl>
1     1  38.0
2    19  79.4
3    20  10.8

Hope that helps,

Luke

this is ok in this example but , is there any way i can update in my function to get the desired result

I don't know anything about SPSS style tables, sorry. You could force a dataframe to have this "looK" but I don't think that's what you're looking for, nor is it a good idea.

I'm not sure what you need (that is, can you use other table tools), but kableExtra, a package I use for tables (mostly because I've used it for years -- there are other very good table packages) allow you to do this kind of stuff with subheadings, etc. The documentation is pretty clear and easy to follow.