<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 8.00.6001.19222"></HEAD>
<BODY style="MARGIN: 4px 4px 1px; FONT: 10pt Tahoma">
<DIV>I've found a very inelegant solution that continues on the path I was going. Using the dummy dataset below, this code will strip neighbors as I desire. </DIV>
<DIV> </DIV>
<DIV>b <- a[0,]</DIV>
<DIV>for(i in 2:nrow(a)){<BR>    if(!is.na(a[match(a$neigh[i],rownames(a)[1:i]),]$ID)){ </DIV>
<DIV>    b <- rbind(b,a[i,])<BR>    }<BR>    } <BR></DIV>
<DIV>> b<BR>     ID dist neigh<BR>3 three  5.1     2<BR>4  four  2.2     1<BR></DIV>
<DIV>I'd still be curious if anyone knows a cleaner solution. </DIV>
<DIV>Best, </DIV>
<DIV>Tim</DIV>
<DIV><BR>>>> Tim Howard 2/20/2013 10:19 AM >>><BR></DIV>
<DIV>I am trying to remove spatial 'duplicates' from a point dataset. The coordinates won't be exactly the same and so I can't use the normal methods for removing the second instance of the points. This generalizes to a question about removing points nearby others, either randomly or based on other criteria (in my case, I want to keep the one with a more recent date attribute). </DIV>
<DIV> </DIV>
<DIV>Although my research and fiddling has got me close, I wonder if there already is a solution I'm missing within the various spatial packages so I'm starting with sig-geo, even though I'm stuck at a spot that would use regular R syntax. </DIV>
<DIV> </DIV>
<DIV>My approach (code at bottom of email):</DIV>
<DIV> </DIV>
<DIV>1. Move the full point data set over to SpatialPoints as decimal degrees longlat  [package=sp]</DIV>
<DIV>2. Reproject to utm, using spTransform</DIV>
<DIV>3. convert to ppp</DIV>
<DIV>4. find the distance from each point to its nearest using nndist()  [package = spatstat]</DIV>
<DIV>5. identify that nearest using nnwhich()  [package = spatstat]</DIV>
<DIV>6. extract those with neighbors closer than 100m</DIV>
<DIV> </DIV>
<DIV>** this is where I'm stuck **  I now have a list of neighbors, for which I'd like to keep the first case of each neighbor but remove the second (and sometimes third). Similar to unique(). Here's a dummy example</DIV>
<DIV> </DIV>
<DIV>#set up dummy data frame, the dist and neigh columns are from nndist() and nnwhich(), respectively</DIV>
<DIV>a <- data.frame(ID=c("one","two","three","four"),dist=c(2.2,5.1,5.1,2.2),neigh=c(4,3,2,1))</DIV>
<DIV> </DIV>
<DIV>#here's as far as I've got, I can remove the neighbor to row one with the following line.</DIV>
<DIV>#a looping solution seems problematic as the size of the dataframe changes with each loop</DIV>
<DIV> </DIV>
<DIV>b <- a[-match(a$neigh[1], rownames(a)),]</DIV>
<DIV> </DIV>
<DIV>Questions:</DIV>
<DIV>- Is there already a function in a spatial package that offers a way to remove points within a certain distance of others?</DIV>
<DIV>- if not, does anyone have any hints for taking the next step from what I've done?</DIV>
<DIV> </DIV>
<DIV>### code for what I've got so far.</DIV>
<DIV>### dat.wind.tall is the input DF of lat long decimal degree coordinates</DIV>
<DIV> </DIV>
<DIV>library(sp)<BR>library(spatstat)<BR>library(maptools)<BR>library(rgdal)<BR>llCRS <-  CRS("+proj=longlat +datum=NAD83")<BR>wind.sp <- SpatialPoints(dat.wind.tall[,c(66,65)], proj4string=llCRS)<BR>prjNew <- CRS("+proj=utm +zone=18 +datum=NAD83")<BR>wind.utm <- spTransform(wind.sp, prjNew)<BR>wind.ppp <- as(as(wind.utm, "SpatialPoints"), "ppp")<BR>turb.dist <- nndist(wind.ppp)<BR>turb.nearest <- nnwhich(wind.ppp)<BR>dat.wind.tall.nbr <- cbind(dat.wind.tall, nneigh=turb.nearest, dist=turb.dist)<BR>closeNeighbors <- dat.wind.tall.nbr[turb.dist<100,]</DIV>
<DIV>#code for removing neighbor to first row. <BR>y <- closeNeighbors[-match(closeNeighbors$nneigh[1], rownames(closeNeighbors)),]<BR></DIV>
<DIV> </DIV>
<DIV>Thanks in advance for any help. </DIV>
<DIV>Tim</DIV></BODY></HTML>