Re: [R] Spatial join between two datasets using x and y co-ordinates

From: Steven McKinney <smckinney_at_bccrc.ca>
Date: Tue, 06 May 2008 18:38:32 -0700

>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org on behalf of Yasir Kaheil
> Sent: Tue 5/6/2008 4:24 PM
> To: r-help_at_r-project.org
> Subject: Re: [R] Spatial join between two datasets using x and y co-ordinates
>
>
> vector dat1.select is the selected records from dat1 by dat2.
>
> > dat1.select<- dat1$x1 %in% dat2$x2 & dat1$y1 %in% dat2$y2
> > dat1[dat1.select,]
> x1 y1 descript
> 1 1824615 5980732 cat
> 2 1823650 5983220 dog
>

This won't always work, for example if another observation in dat1 had the x coordinate of one of the dat2 entries and the y coordinate of another:

> x1<-c(1824615,1823650,1821910,1823650)
> y1<-c(5980732,5983220,5990931,5980732)
> descript<-c("cat", "dog", "horse", "cow")
> dat1<-data.frame(x1,y1,descript)
>
> x2<-c(1824615,1823650)
> y2<-c(5980732,5983220)
> dat2<-data.frame(x2,y2)
>
> dat1

       x1      y1 descript
1 1824615 5980732      cat
2 1823650 5983220      dog
3 1821910 5990931    horse
4 1823650 5980732      cow

> dat2
x2 y2

1 1824615 5980732
2 1823650 5983220
>
> dat1.select<- dat1$x1 %in% dat2$x2 & dat1$y1 %in% dat2$y2
> dat1[dat1.select,]
       x1      y1 descript
1 1824615 5980732      cat
2 1823650 5983220      dog
4 1823650 5980732      cow

>

merge() uses the key variables pasted together, to form a unique key, e.g.

> dat1[match(paste(dat1$x1, dat1$y1, sep = "\r"), paste(dat2$x2, dat2$y2, sep = "\r"), nomatch = 0),]

       x1      y1 descript
1 1824615 5980732      cat
2 1823650 5983220      dog

see e.g. merge.data.frame

Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre

email: smckinney +at+ bccrc +dot+ ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada

>
>
> Andrew McFadden wrote:
> >
> > Hi R users
> >
> > I am trying to create a spatial join between two datasets.
> >
> > The first data set is large and contains descriptive data including x
> > and y co-ordinates.
> >
> > The second dataset is small and has been selected spatially. The only
> > data contained within the second dataset is the x and y coordinates only
> > i.e. no descriptive data.
> >
> > The aim of a join made between the two datasets is to select those
> > points (and hence all the descriptive data) from dataset one that has
> > the same x and y co-ordinates as dataset two.
> >
> > x1<-c(1824615,1823650,1821910)
> > y1<-c(5980732,5983220,5990931)
> > descript<-c("cat", "dog", "horse")
> > dat1<-data.frame(x1,y1,descript)
> >
> > x2<-c(1824615,1823650)
> > y2<-c(5980732,5983220)
> > dat2<-data.frame(x2,y2)
> >
> > dat1
> > dat2
> >
> > The aim of the join is to produce:
> >
> > x1 y1 descript
> > 1 1824615 5980732 cat
> > 2 1823650 5983220 dog
> >
> > And therefore limit the data in dataset 1 to that which has the same x
> > and y co-ordinates as dataset 2.
> >
> > Any suggestions?
> >
> > Regards
> >
> > Andy
> >
> >
> > 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.
> >
> >
>
>
> -----
> Yasir H. Kaheil, Ph.D.
> Catchment Research Facility
> The University of Western Ontario
>
> --
> View this message in context: http://www.nabble.com/Spatial-join-between-two-datasets-using-x-and-y-co-ordinates-tp17093486p17093656.html
> Sent from the R help mailing list archive at Nabble.com.



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.

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 Wed 07 May 2008 - 01:43:17 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 Wed 07 May 2008 - 02:30:35 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.

list of date sections of archive