Skip to content
Prev 138860 / 398506 Next

R code for selecting places spatially and by time

Andrew McFadden <Andrew.McFadden <at> maf.govt.nz> writes:
This should certainly be possible.  Your description of the
problem isn't entirely clear to me, but here's a first approximation:

dat <- data.frame(x,y,date)
## data.frame is better than cbind, it can hold dates and locations

infdate <- as.Date("2008-01-01")
infprem <- subset(dat,date==infdate)
otherprem <- subset(dat,date>=infdate)
## or:
elapsed <- dat$date-infdate
otherprem <- subset(dat,elapsed>0 & elapsed<14)
## I'm not sure this is what you wanted in terms
##  of date restrictions, but you could adjust appropriately
dist <- sqrt(outer(infprem$x,otherprem$x,"-")^2+
             outer(infprem$y,otherprem$y,"-")^2)
             
mindist <- apply(dist,2,min)

minval <- 1000 ## (I don't know what the units are??)
prem <- subset(otherprem,mindist<minval)
## or prem <- otherprem[mindist<minval,]