[R] R matching lat/lon pairs from two datasets?

Matthew Dowle mdowle at mdowle.plus.com
Tue Jan 5 15:25:30 CET 2010


Or if there is a requirement for speed or shorter more convenient syntax 
then there is a data.table join.
Basically setkey(data1,V1,V2) and setkey(data2,V1,V2),  then "data1[data2]" 
does the merge very quickly. You probably then want to do something with the 
merged data set,  which you just add in like this "data1[data2,<something>]" 
or like this "data1[data2][,<something>]".  The columns in the key need to 
be either integers, or factors (which are internally integer),  so it would 
be a bit of extra work in this case to store the lat/lon as integer, if 
multiplying by 100 is ok.  Another option for you anyway.

"jim holtman" <jholtman at gmail.com> wrote in message 
news:644e1f321001041828n2f2a160etd597ba7a8bf76ad6 at mail.gmail.com...
> couple of approaches:
>
>> merge(data1, data2, by.x=c("V1", "V2"), by.y=c("V2", "V1"))
>     V1      V2 V3
> 1 47.82 -123.75 11
> 2 47.82 -123.76  8
>> library(sqldf)
>> sqldf("select * from data2 x2, data1 x1 where x2.V1=x1.V2 and
> x2.V2=x1.V1")
>       V1    V2 V3      V1    V2
> 1 -123.76 47.82  8 -123.76 47.82
> 2 -123.75 47.82 11 -123.75 47.82
>>
>
>
> On Mon, Jan 4, 2010 at 7:37 PM, Douglas M. Hultstrand
> <dmhultst at metstat.com>wrote:
>
>> Hello,
>>
>> I am trying to match lat/lon from one dataset with the lat/lon from a
>> second dataset and use that rows data for calculations.  I am using 
>> match,
>> but this is finding the first match and not comparing the pair, how can I
>> determine if the lat/lon are the same?  See example below.  Is there a
>> better way to determine to a matching pair of lat/lon values?
>>
>> Example Datasets:
>> > data2
>>      V1    V2 V3
>> 1 -123.76 47.82  8
>> 2 -123.75 47.82 11
>>
>> > data[1:2]
>>     V1      V2
>> 1  47.82 -123.76
>> 2  47.82 -123.75
>> 3  47.82 -123.74
>> 4  47.82 -123.73
>>
>> #Subset of current R code :
>> lat <- data$V1
>> lon <- data$V2
>> yrs <- c(1,2,5,10,25,50,100,200,500,1000)
>> lon2 <- data2$V1
>> lat2 <- data2$V2
>> ppt2 <- data2$V3
>>
>> for(i in 1:length(lat2)) {
>>       loc <- match(lat2[i],lat)
>>       loc2 <- match(lon2[i], lon)
>>       print(loc); print(loc2)
>>
>>       #Need to test to make sure loc equals loc2
>>       freq_ppt <-
>> c(data[i,4],data[i,6],data[i,8],data[i,10],data[i,12],data[i,14],data[i,16],data[i,18],data[i,20],data[i,22])
>>       print(freq_ppt)
>>       return_value <- approx(freq_ppt,yrs,xout=data2[i,3])
>>       print(return_value)
>> }
>>
>>
>> Thanks for your help,
>> Doug
>>
>> --
>> ---------------------------------
>> Douglas M. Hultstrand, MS
>> Senior Hydrometeorologist
>> Metstat, Inc. Windsor, Colorado
>> voice: 970.686.1253
>> email: dmhultst at metstat.com
>> web: http://www.metstat.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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
> [[alternative HTML version deleted]]
>



More information about the R-help mailing list