订单或过滤与处理

时间:2014-03-27 19:15:44

标签: r data.table

写作时

dt[a>0, {...}, by=...]

{...}过滤之前或之后处理a>0? (看来答案是之前的)。

我可以想象这两个订单都很有用,所以正确的问题是,我想,如何控制订单或过滤与处理?

1 个答案:

答案 0 :(得分:6)

首先处理i=参数(非常合理),因为您可以使用以下内容进行确认。

library(data.table)

dt <- data.table(a=c(0,1,0,1), grp=c("a", "a", "b", "b"))
#    a grp
# 1: 0   a
# 2: 1   a
# 3: 0   b
# 4: 1   b  

## Show that filtering op in i= is performed before processing in j=
dt[a>0, if(any(a<=0)) stop("a<=0 must've been passed on to j") else a, by=grp]
#    grp V1
# 1:   a  1
# 2:   b  1

## Check that error _is_ thrown when when verboten elements make it past filter 
dt[a<=0, if(any(a<=0)) stop("a<=0 must've been passed on to j") else a, by=grp]
# Error in `[.data.table`(dt, a <= 0, if (any(a <= 0)) \\
# stop("a<=0 must've been passed on to j") else a,  : 
#   a<=0 must've been passed on to j

要执行第二次过滤操作,只需将其置于第二次调用[.data.table()

dt[,tot:=sum(a),by=grp][a>0,]
#    a grp tot
# 1: 1   a   1
# 2: 1   b   1