Skip to content

Instantly share code, notes, and snippets.

@oscarperpinan
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarperpinan/569cbc3f11f859f6e213 to your computer and use it in GitHub Desktop.
Save oscarperpinan/569cbc3f11f859f6e213 to your computer and use it in GitHub Desktop.
library(raster)
library(zoo)
## Indice temporal completo
tt <- seq(as.Date('2015-01-01'), as.Date('2015-12-31'), by = 'month')
tt <- as.yearmon(tt)
## Datos disponibles: indice temporal incompleto
tt1 <- tt[-c(5, 8, 9)]
f <- system.file("external/test.grd", package="raster")
s1 <- stack(lapply(tt1, function(i)raster(f)))
s1 <- setZ(s1, tt1)
## Creamos un RasterStack con NA para rellenar los meses sin datos
tt2 <- tt[!(tt %in% tt1)]
rNA <- raster(f)
rNA[] <- NA
s2 <- stack(lapply(tt2, function(i) rNA))
s2 <- setZ(s2, tt2)
## Unimos los dos RasterStack
s <- stack(s1, s2)
s <- setZ(s, c(getZ(s1), getZ(s2)))
names(s) <- as.character(getZ(s))
## Hay que reordenar el resultado a partir de su índice temporal
s <- s[[order(getZ(s))]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment