2013-10-09 79 views
2

我花了分层随机抽样了raster层,使用R的raster包和sampleStratified功能:[R填充插值矩阵,NA的

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个值,所以一个快速的方法将非常感激。

+1

我不熟悉的空间包,但通常的做法是使用'merge'或者用'data.table'进行连接。 – Roland

回答

1

我会想到相反的方式。除了可能很昂贵的插值之外,您已经知道要更改的单元格不是随机样本中的单元格。因此使用您的随机样本作为您的不要想要更改的单元格编号的索引向量,并对那些未出现在分层样本中的单元格索引使用[<-替换方法。我们对基本功能[<-%in%以及​​使用raster方法。原谅稍微冗长的例子,更好地展示步骤。应该是比较快的,我不设想任何问题与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

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