library(data.table)
DT1 <- data.table(num = 1:6, group = c(\"A\", \"B\", \"B\", \"B\", \"A\", \"C\"))
DT2 <- data.table(group = c(\"A\", \"B\", \"C\"))
<
This is how I would do it: first count the number of times each group appears in DT1
, then simply join DT2
and DT1
.
require(data.table)
DT1 <- data.table(num = 1:6, group = c("A", "B", "B", "B", "A", "C"))
DT2 <- data.table(group = c("A", "B", "C"))
#solution:
DT1[,num_counts:=.N,by=group] #the number of entries in this group, just count the other column
setkey(DT1, group)
setkey(DT2, group)
DT2 = DT1[DT2,mult="last"][,list(group, popular = (num_counts >= 2))]
#> DT2
# group popular
#1: A TRUE
#2: B TRUE
#3: C FALSE