我有以下数据框:
id total_transfered_amount day
1 1000 2
1 2000 3
1 3000 4
1 1000 1
1 10000 4
2 5000 3
2 6000 4
2 40000 2
2 4000 3
2 4000 3
3 1000 1
3 2000 2
3 3000 3
3 30000 3
3 3000 3
需要分别使用dplyr软件包过滤掉每个id的'total_transfered_amount'列中位于90%以上的行,例如,我需要过滤以下行:
2 40000 2
3 30000 3
我们可以用 data.table
library(data.table)
setDT(df1)[,.SD[quantile(total_transfered_amount, 0.9) <
total_transfered_amount] , by = id]
# id total_transfered_amount day
#1: 1 10000 4
#2: 2 40000 2
#3: 3 30000 3
或者我们可以使用 base R
df1[with(df1, as.logical(ave(total_transfered_amount, id,
FUN=function(x) quantile(x, 0.9) < x))),]
# id total_transfered_amount day
#5 1 10000 4
#8 2 40000 2
#14 3 30000 3
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句