Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repeat same raster layer to create a raster stack

Tags:

r

raster

I am trying to create a raster stack from a rasterlayer, where the raster stack is just the same raster layer repeated a certain number of times.

I can do something like this:

library(raster)
rasterstack <- addLayer(rasterlayer, rasterLayer, rasterLayer) 

and this works. However, i want the stack to be about a 1000 layers. I guess i could just loop through, but i was wondering if there was a more sophisticated way to doing this.

The reason I am trying to do this is to calculate the weighted mean of a raster stack with data where each layer is a different time period, and where the weights are in a different raster layer object. I am hoping that if I create a rasterstack from the weights raster layer with the same number of layers as the data, I'll be able to do something like:

  weightedmean <- weighted.mean( data.RasterStack, weights.RasterStack )
like image 318
user2872602 Avatar asked Dec 18 '13 23:12

user2872602


1 Answers

Example data

library(raster)
r <- raster(ncol=10, nrow=10, vals=1:100)

Solution

n <- 10  # number of copies
s <- stack(lapply(1:n, function(i) r)) 

Or

s <- stack(replicate(n, r))
like image 60
Robert Hijmans Avatar answered Oct 04 '22 21:10

Robert Hijmans