【发布时间】:2015-02-05 10:30:11
【问题描述】:
如何在不执行sum 或mean 之类的功能的情况下重塑data.table(长到宽)?
我在看 dcast/melt/reshape/等。
但我没有得到想要的结果。
这是我的数据:
DT <- data.table(id = c("1","1","2","3"), score = c("5", "4", "5", "6"))
原始格式:
> DT
id score
1 5
1 4
2 5
3 6
所需格式:
id score1 score2
1 5 4
2 5 NA
3 6 NA
我现在用:
DT <- DT[, list(list(score)), by=id]
但是第一个单元格的内容是这样的:
c("5", "4")
而且我需要拆分它(我使用包splitstackshape):
DT <- cSplit(DT, "V1", ",")
这可能不是最有效的方法... 有什么更好的方法?
【问题讨论】:
标签: r data.table reshape reshape2 splitstackshape