如何将矢量的简单代码应用于几个栅格?

时间:2013-02-13 15:50:21

标签: r binary raster moving-average

我有12个二进制(光栅)文件。 我想计算12个文件中每个像素的12个值的移动平均值。

对于一个简单的向量,我们可以使用它来获得移动平均值:

         x <- c(1,2,3,NA,NA,4,6,5,6,4,2,5)
        movingmean <- rollapply(x, 3, FUN = mean, na.rm = T,fill=NA)

现在我想做同样的事情,但是用栅格做了我试过了:

files   <- list.files("C:final-2010", "*.envi", full.names = TRUE)
results <- list()
for (.files in files) {
    # read in the 12 files as a vector of numbers 
    # that we take the average of
    x <- do.call(rbind,(lapply(.files, readBin  , double() , 
                     size = 4 ,n =1440 * 720 , signed = T)))
    # take the moving average across the 12 values 
    # from the 12 files for each pixel
    results[[length(results) + 1L]] <- rollapply(x, 3, FUN = mean,na.rm = T)
}

但得到了这个错误:

    Error in seq.default(start.at, NROW(data), by = by) : 
                             wrong sign in 'by' argument

1 个答案:

答案 0 :(得分:1)

也许这样的事情会起作用

results <- overlay(stack(files), fun=function(x) 
                   movingFun(x, fun=mean, n=3, na.rm=TRUE))