[R-sig-Geo] creating rasterStack from a matrix

Robert J. Hijmans r.hijmans at gmail.com
Sat Jul 10 05:35:51 CEST 2010


Steven,

Not that I can think of. Something for the to-do list.

Below is how you could make a RasterBrick (which should be preferable
to a RasterStack for objects created in memory (and otherwise their
behavior is similar to a RasterStack).

globe<-brick(nrows=36, ncols=72)
id <- data.frame(cell=1:ncell(globe))
idv = merge(id, stackmatrix, by=1, all.x=T)
globe <- setValues(globe, as.matrix(idv))

Robert


On Fri, Jul 9, 2010 at 3:21 PM, steven mosher <moshersteven at gmail.com> wrote:
> Assuming I have a matrix of  Nrows and Mcols  where
> Column #1 is filled with a valid cell number for a raster. ALL cell numbers
> will not
>  be present.
>
> Subsequent columns (2-M) contain the data that I want to place in a sequence
> of
> layers, so that in the end I have a rasterStack of M layers. M can be quite
> large,
> ( like 1000-2000) Essentially they are values at various time slices where
> time
> is M.
>
> In any case, if there is only one layer ( column 2) then assigning the
> values in column
> 2 to the cells in column 1 is straightforwd (assuming I read the manual
> correctly.
>
> That works like this:
> ( Create a dummy version of the data with 200 cells)
>
> stackmatrix<-matrix(NA,nrow=200,ncol=3)
> # fake values
>  stackmatrix[,3]<-seq(0,199,by=1)
>  stackmatrix[,2]<-seq(-59.9,-79.8,by=-.1)
> # toss some valid cell numbers in column 1
>  stackmatrix[,1]<-seq(100,299,by=1)
> globe<-raster(nrows=36, ncols=72, xmn=-180, xmx=180, ymn=-90, ymx=90,
> +   projs="+proj=longlat +datum=WGS84")
> # assign the values in column 2 to the cells in column 1.
> globe[stackmatrix[,1]]<-stackmatrix[,2]
>
>
> If I have a large number of columns and I want to create a large rasterStack
> with all the data, is there a slick way to do it
>
> Perhaps apply?
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> 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