我data.table
在R中有一个如下
Col1 Col2
Col1Value1 Col2Value1
Col1Value1 Col2Value2
Col1Value1 Col2Value3
Col1Value2 Col2Value1
Col1Value2 Col2Value3
Col1Value3 Col2Value1
Col1Value3 Col2Value2
Col1Value3 Col2Value3
我想获取给定值Col1 - (Col1Value1,Col1Value2)
与值之间的每种组合的记录计数,Col2 - Col1(Col2Value1,Col2Value2)
如果没有组合记录,则返回0
counts <- dt[, length(unique(Col2)), by=.(Col1, Col2)]
上面的代码返回所有组合,但是
- A combination with 0 records are not returned
- Not able to restrict to a given list
预期结果
Col1 Col2 Count
Col1Value1 Col2Value1 1
Col1Value1 Col2Value2 1
Col1Value2 Col2Value1 1
Col1Value2 Col2Value2 0
在基数R中,您可以执行以下操作:
data.frame(table(dt))
Var1 Var2 Freq
1 Col1Value1 Col2Value1 1
2 Col1Value2 Col2Value1 1
3 Col1Value3 Col2Value1 1
4 Col1Value1 Col2Value2 1
5 Col1Value2 Col2Value2 0
6 Col1Value3 Col2Value2 1
7 Col1Value1 Col2Value3 1
8 Col1Value2 Col2Value3 1
9 Col1Value3 Col2Value3 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句