I have two data.tables. I want to use the keys in one to count the occurrences in another.
DT1 <- data.table(A = c("v1","v2","v1","v2","v3","v4","v5","v6","v1","v2"),
B = c(1,11,111,2,22,222,3,33,333,4))
DT2 <- data.table(C = c("v1","v3","v99"))
setkey(DT1,A)
setkey(DT2,C)
Use DT2 to find rows in DT1 returns an outer join.
nrow(DT1[DT2,]) #returns 5 (v1*3, v3*1, v99) "v99" not in DT2
DT1[DT2,]
A B
1: v1 1
2: v1 111
3: v1 333
4: v3 22
5: v99 NA
Is it possible to force to left join with the same syntax?
DT1[DT2, nomatch = 0L]
? – mtotomerge(DT1, DT2, by.x = "A", by.y = "C", all = TRUE)
. Andv99
is present inDT2
– David Arenburgbase::tabulate
allows the user to include them (by tweakingnbins
). This is one case where dt's functionality is less thanbase
. – smci