Skip to content

How to negate %in%

5 messages · Jim Burke, Torleif Markussen Lunde, Jo Frabetti +1 more

#
I can subset a "SpatialPolygonsDataFrame" from a
data frame containing a smaller subset of IDs. For
example below.

smaller_sp <- large_sp [large_sp$ID %in% smaller_df$ID,]

Given the above how can I do the logically opposite from
the %in% operation and get all those IDs not %in%?

I am processing two different sets of polygons (A,B) in
a loop. Operation is seeing in center points in polys B
fall in A. I would like to remove the spatial polygons
that I found (B) as I traverse the geographical area.

Thanks,
Jim Burke
#
Hi 

This (!) might work:

aa <- 1:10
bb <- 5:6

aa[!aa %in% bb]
[1]  1  2  3  4  7  8  9 10

aa[aa %in% bb]
[1] 5 6

Best wishes
Torleif
On Monday 25 May 2009 08:13:40 pm Jim Burke wrote:
#
Hi All,

Everyone's suggestions work. There are lots
of roads to solutions in R. This is wonderful!

It seems that Dan's and Pedro's solutions
work faster for my largish 1,897 spatial
polygon data frame.

Thanks,
Jim Burke

All replies are below.

=================================================
Hi This (!) might work:
aa <- 1:10
bb <- 5:6
aa[!aa %in% bb]     ## Yes it works
[1]  1  2  3  4  7  8  9 10
aa[aa %in% bb]
[1] 5 6
Best wishes
Torleif Markussen

===================================================
smaller_sp <- large_sp [!(large_sp$ID %in% smaller_df$ID),]
Dan Pulter

====================================================
smaller_sp <- large_sp [large_sp$ID %in% smaller_df$ID == FALSE,]
Pedro Mardones
Torleif Markussen Lunde wrote:
#
On 2009-May-25 , at 15:34 , Jim Burke wrote:

            
There's even more. In the package Hmisc, there is a %nin% command that  
does what you want. It is less standard (requires an additional  
package) but more syntactically pleasing.

JiHO
---
http://jo.irisson.free.fr/
1 day later
#
I like to use this one (albeit probably not the most efficient),

`%ni%` <- Negate(`%in%`)

baptiste
On 25 May 2009, at 20:13, Jim Burke wrote: