I need to efficently create a new column that is a list of named column inputs. I do not know the names of the columns to input them directly so I need to be able to specify them by using a vector. I have an existing method that uses rowwise and c_across but it is very slow when used in large data.frame.
df <- data.frame(id=c(23,24,25,26), col_1=c(45,56,7,NA), col_2=c(56,23,222,56), col_3=c(89,NA,NA,NA))
col_names_vector <- colnames(df)[-1]
list_col_df <- df %>%
rowwise() %>%
mutate(list_col=list(c_across(all_of(col_names_vector))))
Once I have the data in a list I need to be able to extract either the first, last, minimum or maximum none NA value and then find it's posistion in the list. I use that index with another vector. I have tried using nest() but as that produces a data frame I cannot perform the operations I require on a data frame column.
Any thoughts on ways of improving this code?