[R-sig-Geo] grid overlay

Tom Gottfried tom.gottfried at tum.de
Mon Sep 12 09:24:32 CEST 2011


Hi Josh,

Am 09.09.2011 18:16, schrieb Josh Tewksbury:
> Hello - I am new to spatial stats in r.
>
> two lat long grids, one with higher grain, attempting to coerce the course
> grid into #the fine grid through an overlay process.
>
> here is some sample code
>
> x<-c(1,1.5,2,2.5,3,3.5,4,4.5,5.5)
> latA<-rep(x,each=9)
> y<-c(80,81,82,83,84,85,86,87,88)
> lonA<-rep(y,9)
> gridA<-cbind(latA,lonA)
> gridA<-data.frame(gridA)
>

Have a look at ?expand.grid to see how you can get that easier.

>>
>> #producing:
>> #>  gridA[1:12,]
>> #   latA lonA
>> #1   1.0   80
>> #2   1.0   81
>> #3   1.0   82
>> #4   1.0   83
>> #5   1.0   84
>> #6   1.0   85
>> #7   1.0   86
>> #8   1.0   87
>> #9   1.0   88
>> #10  1.5   80
>> #11  1.5   81
>> #12  1.5   82
>
>   x1<-c(1,2.6,4.2,5.8,7.4)
> latB<-rep(x1,each=5)
> y1<-c(80,82.1,84.2,86.3,88.4)
> lonB<-rep(y1,5)
> val_B<-sample(1:49,25,replace=F)
> gridB<-cbind(latB,lonB,val_B)
> gridB<-data.frame(gridB)
>
>>
>> #producing:
>> #>  gridB[1:12,]
>> #   latB lonB val_B
>> #1   1.0 80.0    40
>> #2   1.0 82.1     7
>> #3   1.0 84.2    19
>> #4   1.0 86.3    34
>> #5   1.0 88.4    36
>> #6   2.6 80.0    39
>> #7   2.6 82.1    38
>> #8   2.6 84.2    20
>> #9   2.6 86.3    29
>> #10  2.6 88.4     1
>> #11  4.2 80.0    22
>> #12  4.2 82.1     5
>> #etc....
>
> my objective is to add a column to gridA that contains the value from
> gridB$val_B that is closest in coordinates (lat and lon) to the coordinates
> in gridA
>
> my actual data is about 30,000 lines long, but the method should not change.
> I know there is an easy way to do this - is this in sp?

Yes it is:

library(sp)
coordinates(gridA) <- 1:2; gridded(gridA) <- TRUE; fullgrid(gridA) <- TRUE
coordinates(gridB) <- 1:2; gridded(gridB) <- TRUE; fullgrid(gridB) <- TRUE
gridA <- SpatialGridDataFrame(gridA, over(gridA, gridB))

But there might be more appropriate means in package raster.

Regards,
Tom

>
> thanks!
>
>
>> --
>> Joshua J. Tewksbury
>> Walker Professor of Natural History,
>> Department of Biology, University of Washington
>> 106 Kincaid Hall, Box 351800
>> Seattle WA 98195-1800
>> my lab and office are in 528 Kincaid Hall
>> cell phone 206/331-1893,
>> office phone: 206/616-2129
>> lab phone: 206/616-2132,
>> fax: 206/616-2011
>> website: http://faculty.washington.edu/tewksjj/index.html
>>
>> Please join the Natural History Network (
>> http://www.naturalhistorynetwork.org/)
>>
>>
>>
>>
>
>

-- 
Technische Universität München
Department für Pflanzenwissenschaften
Lehrstuhl für Grünlandlehre
Alte Akademie 12
85350 Freising / Germany
Phone: ++49 (0)8161 715324
Fax:   ++49 (0)8161 713243
email: tom.gottfried at wzw.tum.de
http://www.wzw.tum.de/gruenland



More information about the R-sig-Geo mailing list