[R-sig-Geo] fill layers of a RasterStack with values in rows of a matrix

Robert J. Hijmans r.hijmans at gmail.com
Fri Jun 6 05:23:55 CEST 2014


Vero,

Here is a simpler solution:

library(raster)
m <- matrix(data = runif(n = 9216*506, min = 0, max = 1000), nrow =
9216, ncol = 506)
b <- brick(nrows=96, ncols=96, xmn=-62, xmx=-58, ymn=-49, ymx=-45,
crs="+proj=longlat +datum=WGS84")

b <- setValues(b, m)

Robert

On Tue, May 27, 2014 at 12:54 PM, Veronica Andreo <veroandreo at gmail.com> wrote:
> Hi Andrew, list
>
> I've tried what you suggested and got this:
>
> Error en setValues(r2, m[, i]) :
>   length(values) is not equal to ncell(x), or to 1
>
>
> Thing is my images (rasters i want to reconstruct) are in the rows of
> the matrix (96x96=9216).
>
> So, i tried looping over rows...
>
> l <- lapply(1:nrow(m), function(i) {
>   setValues(r2, m[i,])
> } )
>
> and it works!!!!!
>
> THANK YOU SO MUCH!!!
>
> Best,
> Vero
>
>
>
> 2014-05-27 15:25 GMT-03:00 Andrew Vitale <vitale232 at gmail.com>:
>
>> If I understand right, this should give you what you want:
>>
>> library(raster)
>>
>> # create matrix with specified dimensions and random data
>> m <- matrix(data = runif(n = 9216*506, min = 0, max = 1000),
>>             nrow = 9216, ncol = 506)
>>
>> # create a raster with no values but desired extent/projection/ncells
>> r <- raster(nrows=96, ncols=96, xmn=-62, xmx=-58, ymn=-49, ymx=-45,
>>             crs="+proj=longlat +datum=WGS84")
>>
>> # loop through the columns of the matrix and assign the values to
>> # the raster called r, providing a new raster layer for each time step.
>> # this assumes the cells are stored in the proper order within the matrix.
>>
>> # lapply outputs a list of rasters
>> l <- lapply(1:ncol(m), function(i) {
>>             setValues(r, m[ , i])
>>             } )
>>
>> # stack the list
>> s <- stack(l)
>>
>>
>>
>>
>> On Tue, May 27, 2014 at 10:32 AM, Veronica Andreo <veroandreo at gmail.com>wrote:
>>
>>> Hi list,
>>>
>>> I have a big matrix (9216x506) that is the result of applying dineof (
>>>
>>> http://menugget.blogspot.com.ar/2012/10/dineof-data-interpolating-empirical.html
>>> ),
>>> a method to fill gappy data. In each row I have a vectorized version of my
>>> original images and each column represent a time series for each pixel; so
>>> space in rows and time in columns.
>>>
>>> What I want to do now is to reconstruct my spatio-temporal series of 506
>>> images of 96x96; so I need to use values of each row in the big matrix to
>>> build up a 96x96x506 rasterstack, but I'd like to put it into a loop to
>>> avoid doing it 506 times... It works for just one raster, but I've been
>>> trying for a while but I don't get it to work for the stack.
>>>
>>> This is the approach I'm following:
>>>
>>> #### for one raster layer
>>>
>>> # create the raster object
>>> esqueleto<-raster(nrows=96, ncols=96, xmn=-62, xmx=-58, ymn=-49, ymx=-45,
>>> crs="+proj=longlat +datum=WGS84")
>>> esqueleto<-setValues(esqueleto,Xa[1,])
>>> hasValues(esqueleto)
>>>
>>> [1] TRUE
>>>
>>>
>>> #### for the raster stack
>>>
>>> # create the raster object
>>> esqueleto<-raster(nrows=96, ncols=96, xmn=-62, xmx=-58, ymn=-49, ymx=-45,
>>> crs="+proj=longlat +datum=WGS84")
>>> # create the rasterstack (96x96x506)
>>> list_rasters<-replicate(506, esqueleto)
>>> out<-stack(list_rasters)
>>> # attempt to fill the rasterstack
>>> for (i in 1:506) out[,,i] <- setValues(out,Xa[i,],layer=i)
>>> Error en .local(x, i, j, ..., value) : unused argument (value)
>>> # where Xa is the big matrix with numeric data and row.names the original
>>> images filenames
>>>
>>> > class(Xa)[1] "matrix"> str(Xa) num [1:506, 1:9216] 1.068 1.063 1.536
>>> 1.473 0.806 ...
>>>  - attr(*, "dimnames")=List of 2
>>>   ..$ : chr [1:506] "A20030012003008.L3m_8D_CHL_chlor_a_4km_arg"
>>> "A20030092003016.L3m_8D_CHL_chlor_a_4km_arg"
>>> "A20030172003024.L3m_8D_CHL_chlor_a_4km_arg"
>>> "A20030252003032.L3m_8D_CHL_chlor_a_4km_arg" ...
>>>   ..$ : NULL
>>>
>>>
>>> Can you please help me? Which would be the right approach to get what i
>>> want??
>>>
>>> Thanks so much in advance!
>>>
>>> Best,
>>> Vero
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>>
>>
>> --
>> *Andrew P. Vitale*
>> Masters Student
>> Department of Geography
>> University of Nevada, Reno
>> vitale232 at gmail.com
>>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list