N <- 10000
dat <- data.frame(y=rnorm(N), x=rnorm(N),id=factor(sample(N/100,N,replace=TRUE)))
# It's not the same as and'ing the conditions:
ss <- chainsubset(x < mean(y), y < 3*mean(x))
sum(eval(ss,dat))
sum(evalq(x < mean(y) & y < 3*mean(x), dat))
ss2 <- chainsubset(x < mean(y), y < a*mean(x), out.vars='a')
a <- 3; sum(eval(ss2, dat))
a <- 2; sum(eval(ss2, dat))
# Among observations with x < y, find entire id's with more than
# one fifth of their x's larger than 1/2
ss3 <- chainsubset( x < y, tapply(x,id,function(.xx) {sum(.xx > 1/2) > length(.xx)/5} )[id])
sum(eval(ss3,dat))
Run the code above in your browser using DataLab