2013-02-20 63 views
1

我正在用光栅文件进行一些计算,特别是计算移动平均值。 我想知道热在任何计算之前将值赋给NA。如何在计算之前用几个栅格中的NA替换某些值?

Here is the code : 
files <- list.files("C:final-2010", "*.envi", full.names = TRUE) 
files[round(files,3) == -339999995214436420000000000000000000000.000 ] <- NA 
d1 <- overlay(stack(files),fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE)) 

但我得到了一个错误:

  Error in round(files, 3) : Non-numeric argument to mathematical function 

我想这也:

f=stack(files) 
    f[round(f,3) == -339999995214436420000000000000000000000.000 ] <- NA 
    movi <- overlay(stack(f),fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE)) 

没有错误,但是当我看了看,结果我发现,什么也没有改变。

+1

'files'只包含文件名,而不是在文件中的数据的特征向量。你必须先读取数据。 – juba 2013-02-20 12:46:46

回答

4

这是如何将NA设置为单个栅格图层中的值。一旦你这样做,你可以堆放广告libidum。

library(raster) 
r1 <- raster(nrows=108, ncols=21, xmn=0, xmx=10) 
r1[] <- runif(ncell(r1)) 
par(mfrow = c(1, 2)) 
plot(r1) 
r1[500:1000] <- NA 
plot(r1) 

enter image description here

r <- stack(r1, r1, r1) 
x <- list(c(100, 300), c(400, 600), c(800, 1000)) 
s <- mapply(FUN = function(x, y) { 
  y[x[1]:x[2]] <- NA 
  y 
}, x = x, y = r) 

plot(stack(s)) # not shown here 
+0

'RasterStack'可以被认为是一个列表。我已经添加了一个例子。 – 2013-02-20 14:02:57

+0

这是一个匿名函数。 'x'和'y'也可以是'arg1'和'arg2'。 – 2013-02-20 17:56:53

相关问题