I have a data.table in R as below
Col1 Col2
Col1Value1 Col2Value1
Col1Value1 Col2Value2
Col1Value1 Col2Value3
Col1Value2 Col2Value1
Col1Value2 Col2Value3
Col1Value3 Col2Value1
Col1Value3 Col2Value2
Col1Value3 Col2Value3
I want to get the count of records for each combination between given values in Col1 - (Col1Value1,Col1Value2) against values in Col2 - Col1(Col2Value1,Col2Value2) and if no records for a combination then return 0
counts <- dt[, length(unique(Col2)), by=.(Col1, Col2)]
The above code returns all combinations, but
- A combination with 0 records are not returned
- Not able to restrict to a given list
Expected result
Col1 Col2 Count
Col1Value1 Col2Value1 1
Col1Value1 Col2Value2 1
Col1Value2 Col2Value1 1
Col1Value2 Col2Value2 0