我有以下dt
set.seed(12048)
CC <- c("A", "A", "A", "B", "B", "C", "A", "B")
PO <- c("PO1", "PO2", "PO3", "PO4", "PO5", "PO6", "PO7", "PO8")
Rating <- c(1, 3, 3, 2, 4, 2, 1, 2)
dt <- data.table(CC, PO, Rating)
这样就显示出来了:
CC PO Rating
1: A PO1 1
2: A PO2 3
3: A PO3 3
4: B PO4 2
5: B PO5 4
6: C PO6 2
7: A PO7 1
8: B PO8 2
我的意图是有这样一张桌子:
dept Rating1 Rating2 Rating3 Rating4
1: A 2 0 2 0
2: B 0 2 0 1
3: C 0 1 0 0
我知道&#39;融化&#39;功能系列,但是有一种数据表方式吗?
我正在尝试解决这个问题,但显然这是不正确的。
dt[,.(.N,Rating1=nrow(Rating==1),Rating2=nrow(Rating==2),Rating3=nrow(Rating==3),Rating4=nrow(Rating==4)), by=.(CC, Rating)]
感谢任何帮助