repeat same raster layer to create a raster stack

妖精的绣舞 提交于 2019-12-10 18:20:11

问题


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 )

回答1:


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))


来源:https://stackoverflow.com/questions/20670892/repeat-same-raster-layer-to-create-a-raster-stack

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!