R中使用bfastts生成规则栅格时间序列报错问题
解决bfastts批量处理栅格栈像素时报错的问题
报错原因
bfastts函数仅支持输入单个时间序列(向量),直接传入多行多列的矩阵(每行对应一个像素的时间序列)时,函数无法适配多维度输入,导致出现number of items to replace is not a multiple of replacement length错误。
修正代码
# 加载依赖包 library(raster) library(lubridate) library(sf) library(bfastSpatial) library(bfast) library(zoo) data(tura) dates <- getZ(tura) ## 获取影像日期 # 单个像素处理(原逻辑保留) selected_pixel <- 90 s <- bfastts(as.vector(tura[selected_pixel]), dates, type = c("irregular")) # 批量处理所有像素:逐行应用bfastts tura_matrix <- as.matrix(raster::extract(tura, 1:ncell(tura), df = TRUE)[, -1]) # 对每个像素(矩阵的每一行)生成规则时间序列,结果存为列表 s_tura_list <- apply(tura_matrix, 1, function(pixel_values) { bfastts(pixel_values, dates, type = "irregular") }) # 可选:将列表转为矩阵(列对应像素,行对应规则时间点) # s_tura_matrix <- do.call(cbind, s_tura_list)
说明
apply(tura_matrix, 1, ...)遍历矩阵的每一行(即每个像素的原始时间序列值),对每行单独调用bfastts生成规则时间序列- 最终结果
s_tura_list是一个列表,每个元素对应一个像素的规则时间序列对象,方便后续逐个处理或批量分析 - 如果需要矩阵格式的批量结果,可通过
do.call(cbind, s_tura_list)将列表合并为矩阵
内容的提问来源于stack exchange,提问作者user30985
相关产品推荐
相关产品推荐

