I am needing to create a column called "combinations" in my data table that holds all values of the columns from column 4 to the end of my data table. I will use this line of code for multiple data tables and the number of columns will vary from data table to data table, so I do not always know the index number of the last column. The start will always be column 4.
I know of functions that work just fine using multiple column names, but not using multiple column indices. Does anyone know how to do this?
Example of something that would work using column names and not column indices:
mycols<-c("apple", "orange", "banana")
data[, combinations:=paste(mycols, sep=", ")]
Example of something I've tried using column indices that does not work:
ncols<-ncol(data)
my_cols <- data[ , c(4:ncols)]
data[, combinations:=paste(mycols, sep=", ")]
Example data
id number day apple orange banana
1 35 2 red orange yellow
2 12 3 red NA yellow
3 47 5 NA orange yellow
The final result I'm trying to accomplish
id number day apple orange banana combinations
1 35 2 red orange yellow red, orange, yellow
2 12 3 red NA yellow red, NA, yellow
3 47 5 NA orange yellow NA, orange, yellow