I am trying to combine data from a data.table to create a new column that is a list of previous rows. This is easier to see with an example:
dt <- data.table(id = c(1,1,1,1,2,2,3,3,3), letter = c('a','a','b','c','a','c','b','b','a'))
I would like to aggregate this so that the result is
id letter 1: 1 a,a,b,c 2: 2 a,c 3: 3 b,b,a
Intuitively I tried
dt[,j = list(list(letter)), by = id]
but that will not work. Oddly enough, when I go in each case, for example:
> dt[id == 1,j = list(list(letter)), by = id] id V1 1: 1 a,a,b,c
the result is fine ... I feel like I'm missing .SD somewhere or something like that ...
Can someone point me in the right direction?
Thanks!