How can I reshape a data.table
(long into wide) without doing a function like sum
or mean
?
I was looking at dcast/melt/reshape/etc.
But I don't get the desired results.
This is my data:
DT <- data.table(id = c("1","1","2","3"), score = c("5", "4", "5", "6"))
Original format:
> DT
id score
1 5
1 4
2 5
3 6
Desired format:
id score1 score2
1 5 4
2 5 NA
3 6 NA
I now do the trick with:
DT <- DT[, list(list(score)), by=id]
But then the contents of the first cell is like:
c("5", "4")
And I need to split it (I use the package splitstackshape
):
DT <- cSplit(DT, "V1", ",")
This is probably not the most efficient method... What is a better way?