-1

I have a multi year daily raster time series stack (ras_in) and I need to calculate cumulative sums (e.g. day1 = day1; day2 = day1 + day2; day3 = day1 + day2 + day3,...) by year

I am trying to use 'cumsum' function and Zapply as follow:

fun_my <- function(x){cumsum(x)}
out <- Zapply(ras_in, by=year, fun=fun_my, na.rm=T)

but I get the following error:

Error in FUN(newX[, i], ...) : unused argument (na.rm = TRUE)

The I try with:

fun_my <- function(x,na.rm=T){cumsum(x)}

but I get another error:

Error in v[start:end, ] : subscript out of bounds

Any suggestion? Thanks a million

Gianca
  • 109
  • 10
  • 2
    I don't know what `Zapply` is, and I don't know what `ras_in` is. So how could I answer? – Stephen Henderson Feb 06 '18 at 12:19
  • 1
    The error is pretty self-explanatory: `na.rm` is not a valid function argument. Perhaps try removing `NA`s from `ras_in` manually. Also, please read [how to ask](https://stackoverflow.com/help/how-to-ask) questions, and then provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), *including sample data*. – Maurits Evers Feb 06 '18 at 12:34

1 Answers1

1

Reproducible data (raster stack from 2013-01-01 to 2015-12-31):

library(raster)

r <- raster()

s <- stack(lapply(1:1095, function(x) setValues(x = r, values = sample(x = c(NA,1),size = ncell(r), replace = T))))

s <- setZ(s, seq(as.Date('2013-01-01'),as.Date('2015-12-31'), 'day'), 'days')

Subset by year and compute cumulative sum by year:

year <- c('2013', '2014', '2015')

r_list <- list()

for (i in 1:3) {
  r_list[[i]] <- calc(x = subset(s, which(format(getZ(s), "%Y") %in% year[i])), fun = cumsum)
}

The result is something like this (three raster stacks with cumulative sum by day, one for each year):

## [[1]]
## class       : RasterBrick 
## dimensions  : 180, 360, 64800, 365  (nrow, ncol, ncell, nlayers)
## resolution  : 1, 1  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## data source : in memory
## names       : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10, layer.11, ## layer.12, layer.13, layer.14, layer.15, ... 
## min values  :       1,       2,       3,       4,       5,       6,       7,       8,       9,       10,       ## 11,       12,       13,       14,       15, ... 
## max values  :       1,       2,       3,       4,       5,       6,       7,       8,       9,       10,       ## 11,       12,       13,       14,       15, ... 
## 
## 
## [[2]]
## class       : RasterBrick 
## dimensions  : 180, 360, 64800, 365  (nrow, ncol, ncell, nlayers)
## resolution  : 1, 1  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## data source : in memory
## names       : layer.366, layer.367, layer.368, layer.369, layer.370, layer.371, layer.372, layer.373, layer.374, ## layer.375, layer.376, layer.377, layer.378, layer.379, layer.380, ... 
## min values  :         1,         2,         3,         4,         5,         6,         7,         8,         ## 9,        10,        11,        12,        13,        14,        15, ... 
## max values  :         1,         2,         3,         4,         5,         6,         7,         8,         ## 9,        10,        11,        12,        13,        14,        15, ... 
## 
## 
## [[3]]
## class       : RasterBrick 
## dimensions  : 180, 360, 64800, 365  (nrow, ncol, ncell, nlayers)
## resolution  : 1, 1  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
## data source : in memory
## names       : layer.731, layer.732, layer.733, layer.734, layer.735, layer.736, layer.737, layer.738, layer.739, ## layer.740, layer.741, layer.742, layer.743, layer.744, layer.745, ... 
## min values  :         1,         2,         3,         4,         5,         6,         7,         8,         ## 9,        10,        11,        12,        13,        14,        15, ... 
## max values  :         1,         2,         3,         4,         5,         6,         7,         8,         ## 9,        10,        11,        12,        13,        14,        15, ... 
aldo_tapia
  • 1,153
  • 16
  • 27