r - 如何在 R 中拟合时间序列移动平均线?
问题描述
我有一个与示例类似的栅格堆栈,周期为 62 天。我想拟合一个长度等于 62 天的时间序列移动平均线。我怎样才能在 R 中做到这一点?
library(greenbrown)
data("ndvimap")
#here I have an issue as I do not know how to set the length and run the window
#filt<-focal(ndvimap, fun=mean, na.rm=T)#it does not work
谁能帮我?
解决方案
我不明白你是想每 62 层还是每 62 天总结一次堆栈。但是,对于这两种情况,这是一个可能的解决方案:
library(greenbrown)
library(raster)
data("ndvimap")
#do the mean every 62 layers
step <- 62
nlayer <- nlayers(ndvimap)
sequence <-
split(1:nlayer, rep(1:round(nlayer / step), c(
rep(step, 5), nlayer - floor(nlayer / step) * step
)))
res <- lapply(sequence,FUN = function(x){
mean <- calc(ndvimap[[x]],mean)
})
res <- stack(res)
#do the mean every 2 layers (~62 days)
step <- 2
sequence <- split(1:nlayer,rep(1:(nlayer/step),each=step))
res <- lapply(sequence,FUN = function(x){
mean <- calc(ndvimap[[x]],mean)
})
res <- stack(res)
推荐阅读
- ios - 活动指示器忽略延迟功能
- linux - Golang 无法安装包
- python - 如何将二进制字符串列表放入二维数组
- sql-server - 存储过程,子查询返回超过 1 个值
- r - R:带有隐藏字段的 Web 抓取 .aspx 表单,“未知字段名称”错误
- mysql - 错误代码:1822。添加外键约束失败。引用表 'Works' 中的约束 'project_ibfk_1' 缺少索引
- python - Geopandas - 在与多边形相交处分割线,在新链接属性中保留多边形 ID
- python - 如何使用 BeautifulSoup 加快解析速度?
- javascript - 尝试提取连续子数组时返回的循环对象数组
- python - Python在表格行中查找数据