Andrew McFadden
2008-Mar-06 22:40 UTC
[R] R code for selecting places spatially and by time
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) Regards Andrew Andrew McFadden MVS BVSc Incursion Investigator Investigation & Diagnostic Centres - Wallaceville Biosecurity New Zealand Ministry of Agriculture and Forestry Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St Upper Hutt ######################################################################## This email message and any attachment(s) is intended solely for the addressee(s) named above. The information it contains is confidential and may be legally privileged. Unauthorised use of the message, or the information it contains, may be unlawful. If you have received this message by mistake please call the sender immediately on 64 4 8940100 or notify us by return email and erase the original message and attachments. Thank you. The Ministry of Agriculture and Forestry accepts no responsibility for changes made to this email or to any attachments after transmission from the office. ######################################################################## [[alternative HTML version deleted]]
Andrew, You haven't defined the problem clearly, but I will try to guess what you mean...> i.e. what properties are within 3 km from properties infected on > "2008-01-01" over the last 14 days.I'm guessing that each row of your sample data represents one infection event, and you want to find all rows where the date is within 14 days following "2008-01-01" and the location is within 3 km of any of the sites listed as "2008-01-01". Presumably the x and y coordinates are already in units of metres; otherwise use "project" in package rgdal. Then: 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 <- rep(as.Date(c("2008-01-02","2008-01-17","2008-01-01"), format = "%Y-%m-%d"),c(4,4,2)) ## the set of properties or infections or something props <- data.frame(x, y, date) ## find indices of source events src.date <- as.Date("2008-01-01") src.i <- which(date == src.date) ## distances between properties dists <- as.matrix(dist(cbind(x,y))) ## select sites within space and time bounds hits <- FALSE for (i in src.i) { i.hits <- (src.date < date) & (date <= src.date + 14) & (dists[i,] < 3000) hits <- hits | i.hits } ## look at matching sites props[hits,] ## look at distances of matching sites from source sites dists[src.i,hits] It would also be possible to do it more efficiently if that is an issue. On Fri, Mar 7, 2008 at 9:40 AM, Andrew McFadden <Andrew.McFadden at maf.govt.nz> wrote:> > 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) > > Regards > > Andrew > > Andrew McFadden MVS BVSc > Incursion Investigator > Investigation & Diagnostic Centres - Wallaceville Biosecurity New > Zealand Ministry of Agriculture and Forestry > > Phone 04 894 5600 Fax 04 894 4973 Mobile 029 894 5611 Postal address: > Investigation and Diagnostic Centre- Wallaceville Box 40742 Ward St > Upper Hutt > > > ######################################################################## > This email message and any attachment(s) is intended solely for the > addressee(s) named above. The information it contains is confidential > and may be legally privileged. Unauthorised use of the message, or the > information it contains, may be unlawful. If you have received this > message by mistake please call the sender immediately on 64 4 8940100 > or notify us by return email and erase the original message and > attachments. Thank you. > > The Ministry of Agriculture and Forestry accepts no responsibility for > changes made to this email or to any attachments after transmission from > the office. > ######################################################################## > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Felix Andrews / $B0BJ!N)(B PhD candidate Integrated Catchment Assessment and Management Centre The Fenner School of Environment and Society The Australian National University (Building 48A), ACT 0200 Beijing Bag, Locked Bag 40, Kingston ACT 2604 http://www.neurofractal.org/felix/ 3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8
Andrew McFadden <Andrew.McFadden <at> maf.govt.nz> writes:> > > 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,]