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

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Jun 17 20:39:56 CEST 2010


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



More information about the R-sig-Geo mailing list