Say I have a data frame
df <- data.frame(A=c(1,3,1,4,2,2,5,3,1,7,8),b=c(10:20),c=c(20:30))
and I want to extract all the consecutive rows whose sum in column A is 4.
I can run through with rollapply
from package zoo
and easily find out if there are any such rows with
rollapply(df$A,2,function(x)(ifelse(sum(x)==4,print("YES"),print("NO"))))
But I want to be able to just take out the rows for which this condition is true and put them in another data frame
sum4 <- data.frame(A=c(1,3,1,2,2,3,1),B=c(10,11,12,14,15,17,18),C=c(20,21,22,24,25,27,28))
Is this possible, and if so, how?