Selecting column range dynamically for a function

I have a dataframe as pic below,

gender	sector	col1	col2	col3	col4	col5	col6
1	2	1	1	1	1	1	1
2	1	1	1	1			1
1	2		1			1	
2	5		1			1	
1	2		1			1	1
2	5						
1	5					1	
2	1			1			1
2	3	1					
2	2		1			1	
2	1		1		1		1
1	1		1			1	
1	2		1			1	1
2	1		1			1	
2	3						1
2	5						
2	3			1			1
1	2	1	1			1	
1	3		1		1	1	
1	5		1			1	
1	3						1
1	1	1	1			1	1
2	4	1	1	1			1
1	1	1	1	1		1	1
2	4		1	1		1	1
1	1	1	1			1	
2	2	1					
2	1						
2	1			1			1
1	1		1			1	
2	4		1			1	
1	5	1	1	1			1
2	4		1	1			1
1	5		1	1		1	1
2	4		1	1		1	1
1	1		1	1		1	1
2	4	1					
2	1						
2	2	1		1			1

i am trying to create a function like below for SPSS like tables

df <- data
var_list <- c("col1","col2","col3","col4","col5","col6")
grouping_var <- "sector"

df1<-df[var_list] %>% as.data.frame()
grouping_var <- rlang::parse_expr(grouping_var)
var_lab(df[[grouping_var]]) <-""
var_lab(df1[1]) <- ""
var_lab(colnames(df1)[ncol(df1)]) <- ""
data1 <- df %>% select(all_of(grouping_var)) %>% cbind(df1)
tab1 <- data1 %>% 
  tab_cells(mdset(col1 %to% col6)) %>% tab_cols(total(), data1[1]) %>% 
  tab_stat_cpct() %>% tab_pivot()

every thing is working manually.
actually i am trying to give my first value var (col1) and last value var(col6) dynamically, so that function can automatically select first value variable and last value variable to calculate, first column of "data1" will always be grouping variable.

here is a reprex, i use dput to generate text which will best transfer example data.
I parameterised you code to take col1 and col6 as strings in first_col_param and second_col_param

# dput(df)
df <- structure(list(gender = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 
 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 
 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L), sector = c(2L, 
 1L, 2L, 5L, 2L, 5L, 5L, 1L, 3L, 2L, 1L, 1L, 2L, 1L, 3L, 5L, 3L, 
 2L, 3L, 5L, 3L, 1L, 4L, 1L, 4L, 1L, 2L, 1L, 1L, 1L, 4L, 5L, 4L, 
 5L, 4L, 1L, 4L, 1L, 2L), col1 = c(1L, 1L, NA, NA, NA, NA, NA, 
 NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, 1L, 
 1L, NA, 1L, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, NA, 1L
 ), col2 = c(1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, 1L, 1L, 1L, 1L, 
 1L, NA, NA, NA, 1L, 1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, 
 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA), col3 = c(1L, 1L, NA, 
 NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 
 NA, NA, NA, 1L, 1L, 1L, NA, NA, NA, 1L, NA, NA, 1L, 1L, 1L, 1L, 
 1L, NA, NA, 1L), col4 = c(1L, NA, NA, NA, NA, NA, NA, NA, NA, 
 NA, 1L, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 
 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), col5 = c(1L, 
 NA, 1L, 1L, 1L, NA, 1L, NA, NA, 1L, NA, 1L, 1L, 1L, NA, NA, NA, 
 1L, 1L, 1L, NA, 1L, NA, 1L, 1L, 1L, NA, NA, NA, 1L, 1L, NA, NA, 
 1L, 1L, 1L, NA, NA, NA), col6 = c(1L, 1L, NA, NA, 1L, NA, NA, 
 1L, NA, NA, 1L, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L, 1L, 1L, 
 1L, 1L, NA, NA, NA, 1L, NA, NA, 1L, 1L, 1L, 1L, 1L, NA, NA, 1L
 )), row.names = c(NA, -39L), class = c("tbl_df", "tbl", "data.frame"
 ))

library(expss)
library(tidyverse)
library(rlang)
var_list <- c("col1","col2","col3","col4","col5","col6")
grouping_var <- "sector"
first_col_param <- "col1"
second_col_param <- "col6"

df1<-df[var_list] %>% as.data.frame()
grouping_var <- parse_expr(grouping_var)
var_lab(df[[grouping_var]]) <-""
var_lab(df1[1]) <- ""
var_lab(colnames(df1)[ncol(df1)]) <- ""
data1 <- df %>% select(all_of(grouping_var)) %>% cbind(df1)
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()

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Thanks nirgrahanuk, but lets say , if i will do labeling of all variable columns like

df$col1<-factor(df$col1, levels=1, labels="Executive")

so Paste0 will convert it to Col1, do we have any solution for this

df[["col1"]]<-factor(df[["col1"]], levels=1, labels="Executive")

Is the same, so again string containing col1 can be used

Sorry no. I dont understand. But good luck trying to do your thing.

ok, thanks, actually i am saying do we have onother solution like without Paste0 the code will work.

actually Paste0 is giving actual variable(col1,col2) value rather than labelled value after labeling

image

i hope made you understand my issue

Thanks , it works, i have just changed labeling before applying function.
for now its working, thanks for helping
wish to learn a lot from you