我有一个多年的每日栅格时间序列堆栈(ras_in),我需要按年计算累积总和(e.g. day1 = day1; day2 = day1 + day2; day3 = day1 + day2 + day3,...)
我正在尝试使用'cumsum'
函数和Zapply
,如下所示:
fun_my <- function(x){cumsum(x)}
out <- Zapply(ras_in, by=year, fun=fun_my, na.rm=T)
但是我收到以下错误:
FUN中的错误(newX [,i],...):未使用的参数(na.rm = TRUE)
我尝试:
fun_my <- function(x,na.rm=T){cumsum(x)}
但我收到另一个错误:
v [start:end,]:下标超出范围
时出错
有什么建议吗?万分感谢
答案 0 :(得分:1)
可重复数据(2013-01-01至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')
按年份子集并按年计算累积总和:
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)
}
结果是这样的(三个光栅堆栈,每天累计和,每年一个):
## [[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, ...