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?
You can use getanID
to create a unique .id
for the grouping variable id
. Then, try with dcast.data.table
(or simply dcast
from versions 1.9.5 and beyond) and if needed change the column names using setnames
library(splitstackshape)
res <- dcast(getanID(DT, 'id'), id~.id,value.var='score')
setnames(res, 2:3, paste0('score', 1:2))[]
# id score1 score2
#1: 1 5 4
#2: 2 5 NA
#3: 3 6 NA
Or using only data.table
dcast(DT[, .id:=paste0('score', 1:.N), by=id],
id~.id, value.var='score')
# id score1 score2
#1: 1 5 4
#2: 2 5 NA
#3: 3 6 NA
Or from the code you were using (less number of characters)
cSplit(DT[, toString(score), by=id], 'V1', ',')
# id V1_1 V1_2
#1: 1 5 4
#2: 2 5 NA
#3: 3 6 NA