I would like to combine multiple columns to one list column in a data.table in R. Example:
require(data.table)
dt = data.table(col1 = LETTERS[1:5],
col2 = rep('test', 5),
col3 = c('hello', 'yes', 'no', 'maybe', 'why'))
Q: How can I combine col2 and col3 into a list column?
What I've tried so far:
cols = c('col2', 'col3')
dt[ , col4 := paste0(.SD, collapse = ', '), .SDcols = cols,
by = 1:nrow(dt) ] # paste's them together
dt[ , col4 := c(.SD), .SDcols = cols,
by = 1:nrow(dt) ] # drops col3
dt[ , col4 := lapply(.SD, c), .SDcols = cols,
by = 1:nrow(dt) ] # drops col3