关于此主题有很多问答。 我尝试了其中的一些功能,包括count(),length(),unique(),unlist(),... 看起来很简单,但是我找不到解决方案。
让我在一个例子中解释一下, 我有一个数据表:
DT = data.table(x=c("a","a","a","a","a","a","a","a","a","b","b","b","b","b"), y=c(1,1,1,1,2,4,4,4,6,6,6,6,6,7), z=c(1,1,2,3,4,5,6,6,7,8,8,8,9,10))
> DT
x y z
1: a 1 1
2: a 1 1
3: a 1 2
4: a 1 3
5: a 2 4
6: a 4 5
7: a 4 6
8: a 4 6
9: a 6 7
10: b 6 8
11: b 6 8
12: b 6 8
13: b 6 9
14: b 7 10
我想要一个新列t:
DT <- DT[,t := length(count(z)),by=list(x,y)]
当x = a并且y = 1时:z具有3个不同的值
当x = a和y = 2时:z具有1个不同的值
当x = b并且y = 6时:z具有2个不同的值,依此类推...
因此,列t应该像:
x y z t
1: a 1 1 3
2: a 1 1 3
3: a 1 2 3
4: a 1 3 3
5: a 2 4 1
6: a 4 5 2
7: a 4 6 2
8: a 4 6 2
9: a 6 7 1
10: b 6 8 2
11: b 6 8 2
12: b 6 8 2
13: b 6 9 2
14: b 7 10 1
答案 0 :(得分:1)
也许
DT[, t := uniqueN(z), by = list(x, y)][]
正在做您期望的事情。
x y z t 1: a 1 1 3 2: a 1 1 3 3: a 1 2 3 4: a 1 3 3 5: a 2 4 1 6: a 4 5 2 7: a 4 6 2 8: a 4 6 2 9: a 6 7 1 10: b 6 8 2 11: b 6 8 2 12: b 6 8 2 13: b 6 9 2 14: b 7 10 1