How can I drop/keep groups according to a condition in data.table? Is there a better method than adding a new column, then filtering on that column and removing it?
Here is another method that uses .I to return the row indices of selected groups and then subsets on the row:
.I
dt[dt[, .I[2 %in% b], a]$V1] a b 1: 1 5 2: 1 2 3: 1 2 4: 2 3 5: 2 5 6: 2 2