按条件选择组
# example data
DT = data.table(Titanic)
假设我们只想在多数人幸存的情况下才能看到每个类:
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
在这里,只有满足条件,我们才会返回数据子集 .SD
。另一种选择是
DT[, .SD[ sum(N[Survived=="Yes"]) > sum(N[Survived=="No"]) ) ], by=Class]
但这有时被证明是较慢的。