[R] How to Dedup a Spatial Points Data Set

1 view
Skip to first unread message

Paul Heinrich Dietrich

unread,
Aug 18, 2009, 5:10:11 PM8/18/09
to r-h...@r-project.org

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.
--
View this message in context: http://www.nabble.com/How-to-Dedup-a-Spatial-Points-Data-Set-tp25033516p25033516.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Rolf Turner

unread,
Aug 18, 2009, 6:16:04 PM8/18/09
to Paul Heinrich Dietrich, r-h...@r-project.org

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

Reply all
Reply to author
Forward
0 new messages