[R] How to Dedup a Spatial Points Data Set

Rolf Turner r.turner at auckland.ac.nz
Wed Aug 19 00:16:04 CEST 2009


On 19/08/2009, at 9:10 AM, Paul Heinrich Dietrich wrote:

>
> I'm new to spatial analysis and am exploring numerous packages, mostly
> enjoying sp, gstat, and spBayes.
>
> Is there a function that allows the user to dedup a data set with  
> multiple
> values at the same coordinates and replace those duplicated values  
> with the
> mean at those coordinates?  I've written some cumbersome code that  
> works,
> but would prefer an efficient R function if it exists.  Thanks.

Using the spatstat package, and with your data set converted to a ppp  
object
with marks equal to the ``values'', you can avail yourself of the  
following
simple function:

foo <- function(X){
a <- with(X,tapply(marks,paste(x,y),mean))
b <- strsplit(names(a)," ")
x <- as.numeric(sapply(b,function(w){w[[1]]}))
y <- as.numeric(sapply(b,function(w){w[[2]]}))
ppp(x=x,y=y,marks=as.vector(unname(a)))
}

E.g.:

library(spatstat)
set.seed(42)
x <- runif(20)
y <- runif(20)
i <- sample(1:20,100,TRUE)
X <- ppp(x=x[i],y=y[i],marks=rnorm(100))
Y <- foo(X)
plot(unmark(X))
plot(Y,add=TRUE,cols="red")

HTH

	cheers,

		Rolf Turner

P.S. Note that you will have to specify an ***observation window***  
when converting
your data set to a ppp object.  If you haven't a ``naturally  
occurring'' window, you
can create one using the ripras() function to create a window.

		R. T.

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list