# Re: [R] R code for selecting places spatially and by time

From: Ben Bolker <bolker_at_ufl.edu>
Date: Fri, 07 Mar 2008 14:41:18 +0000 (UTC)

>
>
> Hi all
>
> The code of trying to write relates to selecting properties (given by x
> and y co-ordinates) spatially (distance "X" from "infected" properties
> identified by date) over a certain time period.
>
> i.e. what properties are within 3 km from properties infected on
> "2008-01-01" over the last 14 days.
>
> Is any one able to give me some clues on how to write code to solve this
> problem.
>
> Some sample data is as follows:
>
> x<-rep(c(2660156,2660203,2658165,2659303,2661531,2660914),c(2,2,2,2,1,1)
> )
> y<-rep(c(6476767,6475013,6475487,6479659,6477004,6476388),c(2,2,2,2,1,1)
> )
> date<-as.character(rep(as.Date(c("2008-01-02","2008-01-17","2008-01-01")
> ,format = "%Y-%m-%d"),c(4,4,2)))
> cbind(x,y,date)

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,]

R-help_at_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. Received on Fri 07 Mar 2008 - 14:44:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 07 Mar 2008 - 15:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.