问题描述
我的问题与最近一个不幸的未解答问题非常相似发布在 Stackoverflow 上.我正在处理一个由十二层组成的 RasterStack
对象(一年中的每个月一个),我想将这些层复制十次,最终得到一个 RasterStack
由 120 层组成,每 12 层都相似(即,第 1 层与第 13 层相同,与第 25 层相同,以此类推).
My question is pretty similar to an unfortunately unanswered issue recently posted on Stackoverflow. I am dealing with a RasterStack
object consisting of twelve layers (one for each month of the year), and I would like to replicate the layers ten times, ending up with a RasterStack
consisting of 120 layers, with every 12th layer being similar (i.e., layer 1 is the same like layer 13 is the same like layer 25 and so on).
出于复制目的,让我们以 raster
包中的示例为例:
For replication purposes, let's take an example from the raster
package:
library(raster)
file <- system.file("external/test.grd", package = "raster")
s <- stack(file, file, file, file, file, file, file, file, file, file, file, file)
stack(s, s, s, s, s, s, s, s, s, s)
class : RasterStack
dimensions : 115, 80, 9200, 120 (nrow, ncol, ncell, nlayers)
resolution : 40, 40 (x, y)
extent : 178400, 181600, 329400, 334000 (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:28992 +towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs
names : test.1.1, test.2.1, test.3.1, test.4.1, test.5.1, test.6.1, test.7.1, test.8.1, test.9.1, test.10.1, test.11.1, test.12.1, test.1.2, test.2.2, test.3.2, ...
min values : 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, 128.434, ...
max values : 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, 1805.78, ...
当然,它可以像最后一行代码那样手动完成,但这对我来说似乎很不方便.任何关于如何以更好的方式实现我的目标的建议将不胜感激!
Of course, it could be done manually like in the last line of code, but that seems quite inconvenient to me. Any suggestions on how to achieve my goal in a better way would be highly appreciated!
推荐答案
您可能会喜欢这里的 mget
,因为它接受对象名称的 character
向量并返回对象,所以你可以这样做:
You might like mget
here, since it takes a character
vector of object names and returns the object, so you can do this:
big.stack <- stack( mget( rep( "s" , 12 ) ) )
nlayers( big.stack )
#[1] 144
或者如果你不喜欢使用 mget()
或者使用 replicate()
将它们放在一个 list
中,然后将它们堆叠起来list
作为 rasterLayers
的 list
是 stack()
...
Or using replicate()
to put them in a list
if you don't like using mget()
, and then stacking the list
as a list
of rasterLayers
is valid input to stack()
...
ll <- replicate( 12 , s )
big.stack2 <- stack( ll )
identical( big.stack , big.stack2 )
#[1] TRUE
这篇关于多次堆叠现有的 RasterStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!