I'm trying to aggregate a data from a data.table to create a new column which is a list of previous rows. It's easier to see by 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 in such a ways that the result should be
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 doesn't work. Oddly enough when I go case by 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 an .SD
somewhere or something like that...
Can anybody point me in the right direction?
Thanks!