Search code examples
rraster

copy rasters within stack


I would like to compare soil moisture rasters available every 3 days to rainfall rasters available daily. I make a stack of each and resample to the appropriate resolution. Now, to compare the stacks easily, it'd be nice to be able to copy each layer in the soil moisture stack and insert it next to itself twice. This is basically the same question as Stacking an existing RasterStack multiple times except that I need the big stack to be sorted so that all of the rasters are in time order. Is there a way to do this?

(I know that I could copy the files before stacking them the 1st time, but this would require resampling 3x the stack. Since resampling is the slowest part of my script, there should be a better way.)


Solution

  • Something like this?

    # example data
    r <- raster(ncol=10, nrow=10)
    r[]=1:ncell(r)
    x <- brick(r,r,r,r,r,r)
    x <- x * 1:6
    
    y <- list()
    for (i in 1:nlayers(x)) {
        r <- raster(x, i)
        y <- c(y, r, r, r)
    }
    s <- stack(y)