返回错误

时间:2017-01-14 15:18:18

标签: time-series r-raster linear-interpolation string-substitution

我需要从rasterstack数据中替换或插入每个单元格的向量中的NA。我有两个函数fun_sub用于替换NA和fun_interp用于插值NA。 我发现fun_sub工作得很好。但fun_interp不起作用,但我找不到理由。 非常感谢你

张天翼

下面是一个简单的例子:

#------EXAMPLE
library(timeSeries)
library(raster)
fun_sub <- function(x) {
  # substitute the NA to the mean of vector for each cell
v=as.vector(x)
z<-substituteNA(v,type="mean")
return (z)
}

fun_interp <- function(x) {
  # interpolate the NA to the the linear regression of vector for each cell
  v=as.vector(x)
  z=interpNA(v, method="linear")
  return (z)
}

# create data
r<-raster(ncols=2,nrows=2)
r1<-r; r2<-r; r3<-r; r4<-r
r1[]<-c(1,1,1,2)
r2[]<-c(5,5,NA,5)
r3[]<-c(3,3,4,2)
r4[]<-c(6,5,5,2)
s<-stack(r1,r2,r3,r4)

# try the two functions; the task is change the NA in r2 to a number;
res_sub<-calc(s,fun_sub) # works great! substitute the NA to the mean of    c(1,NA,4,5); I got c(1,3.333,4,5)
res_inter<-calc(s,fun_interp) # cannot interpolate; have an error, don't know the reason; I expected it is c(1, 2.5 ,4, 5). But it returns an error

# try whether interpNA() can work or not
interpNA(c(1,NA,4,5),method="linear") # but this function is OK.

1 个答案:

答案 0 :(得分:0)

我会这样使用标准函数:

# create data
library(raster)
r1 <- r2 <- r3 <- r4 <-raster(ncol=2,nrow=2)
r1[] <- c(1,1,1,2)
r2[] <- c(5,5,NA,5)
r3[] <- c(3,3,4,2)
r4[] <- c(6,5,5,2)
s < -stack(r1,r2,r3,r4)

m <- mean(s, na.rm=TRUE)
r_sub <- cover(s, m)
r_int <- approxNA(s)

values(s)
##     layer.1 layer.2 layer.3 layer.4
##[1,]       1       5       3       6
##[2,]       1       5       3       5
##[3,]       1      NA       4       5
##[4,]       2       5       2       2

values(r_sub)
##     layer.1  layer.2 layer.3 layer.4
##[1,]       1 5.000000       3       6
##[2,]       1 5.000000       3       5
##[3,]       1 3.333333       4       5
##[4,]       2 5.000000       2       2

values(r_int)
##     layer.1 layer.2 layer.3 layer.4
##[1,]       1     5.0       3       6
##[2,]       1     5.0       3       5
##[3,]       1     2.5       4       5
##[4,]       2     5.0       2       2

使用timeSeries包中的函数

library(timeSeries)
library(raster)

新数据。计算测试函数需要至少6个单元格,这是一个问题。

r1 <- r2 <- r3 <- r4 <-raster(ncol=3,nrow=2)
r1[] <- c(1,1,1,2,1,1)
r2[] <- c(0,5,NA,5,1,1)
r3[] <- c(3,3,4,2,1,1)
r4[] <- c(6,5,5,2,1,1)
s <- stack(r1,r2,r3,r4)

values(s)
values(calc(s, function(v) substituteNA(v,type="mean")))
values(calc(s, function(v) interpNA(v, method="linear")))


values(s)
##     layer.1 layer.2 layer.3 layer.4
##[1,]       1       0       3       6
##[2,]       1       5       3       5
##[3,]       1      NA       4       5
##[4,]       2       5       2       2
##[5,]       1       1       1       1
##[6,]       1       1       1       1

values(calc(s, function(v) substituteNA(v,type="mean")))
##     layer.1  layer.2 layer.3 layer.4
##[1,]       1 0.000000       3       6
##[2,]       1 5.000000       3       5
##[3,]       1 3.333333       4       5
##[4,]       2 5.000000       2       2
##[5,]       1 1.000000       1       1
##[6,]       1 1.000000       1       1

values(calc(s, function(v) interpNA(v, method="linear")))
##     layer.1 layer.2 layer.3 layer.4
##[1,]       1     0.0       3       6
##[2,]       1     5.0       3       5
##[3,]       1     2.5       4       5
##[4,]       2     5.0       2       2
##[5,]       1     1.0       1       1
##[6,]       1     1.0       1       1