[R-sig-Geo] creating a mean of ascii grids

Robert J. Hijmans r.hijmans at gmail.com
Thu Jun 17 20:51:25 CEST 2010


Or like this:

library(raster)
files = list.files(path="C:\\junk\\", pattern='.asc')
s = stack(files)
r = mean(s)

plot(r)
r = writeRaster(r, filename='mean.asc')



On Thu, Jun 17, 2010 at 11:39 AM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Thu, Jun 17, 2010 at 7:27 PM, Grant Gillis <grant.j.gillis at gmail.com> wrote:
>
>> grid1 <- readAsciiGrid("C:\\junk\\grid1.asc")
>> grid2 <- readAsciiGrid("C:\\junk\\grid2.asc")
>>
>> After this no luck with the "mean" command
>
>  These things are SpatialGridDataFrames and can be 'cbind'ed together:
>
>  > gAll = cbind(grid1,grid2)
>
>  > spplot(gAll)
>
> now compute the row means and add them to the data frame:
>
>  > gAll$mean = apply(gAll at data[,1:2],1,mean)
>  > spplot(gAll)
>
>  Just to make sure it was doing the right thing I made some test data:
>
>  > gAll at data[,1]=1:100
>  > gAll at data[,2]=100:1
>  > gAll$mean = apply(gAll at data[,1:2],1,mean)
>  > spplot(gAll)
>
>  - which shows me two ramps in opposite directions and a very flat
> mean, with value:
>
>  > gAll at data[,3]
>  [1] 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5
>  [16] 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5 50.5
>
>  How's that? It probably only works if the grids have the same spatial
> basis. If you've really got many of these, then write a loop.
>
> Barry
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list