# example data DT = data.table(Titanic)
Suppose we want to see each class only if a majority survived:
DT[, if (sum(N[Survived=="Yes"]) > sum(N[Survived=="No"]) ) .SD, by=Class] # Class Sex Age Survived N # 1: 1st Male Child No 0 # 2: 1st Female Child No 0 # 3: 1st Male Adult No 118 # 4: 1st Female Adult No 4 # 5: 1st Male Child Yes 5 # 6: 1st Female Child Yes 1 # 7: 1st Male Adult Yes 57 # 8: 1st Female Adult Yes 140
Here, we return the subset of data
.SD only if our condition is met. An alternative is
DT[, .SD[ sum(N[Survived=="Yes"]) > sum(N[Survived=="No"]) ) ], by=Class]
but this has sometimes proven slower.