David B
David B

Reputation: 29978

Filtering a dataframe in R

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?

Upvotes: 0

Views: 2501

Answers (2)

Gary Li
Gary Li

Reputation: 136

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)})

Upvotes: 2

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

This is the same question as: 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,]

Upvotes: 5

Related Questions