R用NA填充插值矩阵

时间:2013-10-09 08:36:45

标签: r interpolation raster na

我使用R的raster包和raster函数从sampleStratified图层中取出一个分层随机样本:

library(raster)
r<-raster(nrows=5, ncols=5)
r[]<-c(1,0,0,0,1,1,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0,0,1,1,1)


#Stratified random sample size
sampleStratified(r, size=5)
      cell layer
 [1,]    3     0
 [2,]   22     0
 [3,]    7     0
 [4,]   21     0
 [5,]   12     0
 [6,]   13     1
 [7,]   17     1
 [8,]   11     1
 [9,]    8     1
[10,]   23     1

我现在要做的是按第一列排序样本,插入第一列以获取光栅的原始长度,并用NA填充第二列的缺失值,如下所示:

   [,1] [,2]
 [1,]    1   NA
 [2,]    2   NA
 [3,]    3    0
 [4,]    4   NA
 [5,]    5   NA
 [6,]    6   NA
 [7,]    7    0
 [8,]    8    1
 [9,]    9   NA
[10,]   10   NA
[11,]   11    1
[12,]   12    0
[13,]   13    1
[14,]   14   NA
[15,]   15   NA
[16,]   16   NA
[17,]   17    1
[18,]   18   NA
[19,]   19   NA
[20,]   20   NA
[21,]   21    0
[22,]   22    0
[23,]   23    1
[24,]   24   NA
[25,]   25   NA

我尝试使用approxTime软件包中的simecol函数,但是在填充NA时失败了。我有10个栅格图层,每个图层的值大约为500,000,所以快速的方法非常值得赞赏。

2 个答案:

答案 0 :(得分:1)

我会使用merge作为@Roland建议。

mm <- data.frame(col1 = sample(1:100, 50), col2 = sample(0:1, 50, replace = TRUE))
mm <- as.matrix(mm[order(mm[, 1]), ])
mdl <- as.matrix(data.frame(col1 = 1:100, col2 = NA))
merge(mdl, mm, by = "col1", all.x = TRUE)

    col1 col2.x col2.y
1      1     NA     NA
2      2     NA      0
3      3     NA      0
4      4     NA     NA
5      5     NA     NA
6      6     NA     NA
7      7     NA      0
8      8     NA      1
9      9     NA     NA
10    10     NA      0
11    11     NA     NA
12    12     NA      0
13    13     NA      1

答案 1 :(得分:1)

我会以相反的方式思考它。您可能已经知道要更改的单元格是随机样本中不包含的单元格,而不是昂贵的插值。因此,将您的随机样本用作您想要更改的单元格数的索引向量,并对那些未出现在分层样本中的单元格索引使用[<-替换方法。我们对基函数raster[<-以及%in%使用seq_len方法。原谅稍微啰嗦的例子,更好地展示步骤。应该非常快,我没有设想任何500,000个细胞的栅格问题...

# For reproducible example
set.seed(1)

#  Get stratified random sample
ids <- sampleStratified(r, size=5)

#  Copy of original raster (to visualise difference)
r2 <- r

# Get set of cell indices
cell_no <- seq_len(ncell(r2)) 

# Those indices to replace are those not in the random sample
repl <- cell_no[ ! cell_no %in% ids[,1] ]

#  Replace cells not in sample with NA
r2[ repl ] <- NA

# Plot to show what is going on
par( mfrow = c(1,2))
plot(r)
plot(r2)

enter image description here

相关问题