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 )
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With