DT <- data.table(id = rep(1:3, 2),
class = rep(letters[1:6]),
des = rep(LETTERS[1:2], 3))
which looks like this:
id class des
1: 1 a A
2: 2 b B
3: 3 c A
4: 1 d B
5: 2 e A
6: 3 f B
The question is that I need to stack different value (string type) of variables class & des in each id into one row, that is, how to convert that data.table into the following shape:
id class des
1: 1 a, d A, B
2: 2 b, e B, A
3: 3 c, f A, B
I have tried something like this, but the result is not what I expected.
DT %>%
dcast(id ~ ..., fun = function(x) paste(x, ", "), value.var = c("class", "des"))
id class des
1: 1 d , B ,
2: 2 e , A ,
3: 3 f , B ,