views:

130

answers:

2

I have the following dataframe (df)

 start     end
1    14379   32094
2   151884  174367
3   438422  449382
4   618123  621256
5   698271  714321
6   973394  975857
7   980508  982372
8   994539  994661
9  1055151 1058824
.   .       .
.   .       .
.   .       .

And a long boolean vector with boolean values (vec).

I would like to filter out all ranges in df that contain at least one TRUE value in the corresponding locations in vec.

In other words, a row with start=x end=y will be outputted iff !any(vec[x:y]).

Any ideas on how to accomplish that?

+5  A: 

This is the same question as: http://stackoverflow.com/questions/3651651/adding-a-column-to-a-dataframe-in-r so it has the same answer... use apply, but with any instead of mean...

> ranges <- apply(DF,1,function(row) !any(vec[ row[1]:row[2] ]))
> DF[ranges,]
Joshua Ulrich
+2  A: 

I have read your other posts about this topic, if you want to achieve this with plyr, try this:

new.df <- adply(df, .margins=1, function(x){if(!any(vec[x$start:x$end])) return(x)})
Gary Lee
+1 Thank you for introducing `adply`
David B