2017-10-29 1 views
0

이 질문 (Find second highest value on a raster stack in R)에 이어 두 번째로 높은 값을 가진 레이어의 이름을 각 래스터 스택 xy 좌표에서 어떻게 찾을 수 있습니까? 나는 래스터를 얻을 수있는 방법 그러나래스터 스택에서 두 번째로 큰 값의 레이어 이름 찾기 R

set.seed(123) 
require(raster) 
r1 <- raster(nrows = 10, ncols = 10) 
r2 <- r3 <- r4 <- r1 
r1[] <- runif(ncell(r1)) 
r2[] <- runif(ncell(r1)) + 0.2 
r3[] <- runif(ncell(r1)) - 0.2 
r4[] <- runif(ncell(r1)) 
rs <- stack(r1, r2, r3, r4) 

which.max.na <- function(x, ...) ifelse(length(x) == sum(is.na(x)), 0, which.max(x)) 

m1 <- calc(rs, which.max.na) 

plot(m1) 

: 나는 "which.max()"기능으로 가장 높은 값을 포함하는 레이어의 이름 (레이어 번호를) 찾을 수 있어요

두 번째로 높은 값을 포함하는 이름 (레이어 번호)?

m2 <- calc(rs, fun=function(x, na.rm) x[order(x, decreasing=T)[2]]) & calc(rs, fun=function(x, na.rm) order(x, decreasing=T)[2]) 

plot(m2) 

하지만 plot(m2) 보듯이 성공하지

..

답변

1

여기에서 두 번째로 높은 인덱스를보고하도록 which.max.na 함수를 수정 접근법이다

난 ( How to find second highest value and corresponding layer name in a raster stack in R)의 용액을 시도했다. NA가 하나가 아닌 경우 기능 보고서에 0을보고하도록 sum(!is.na(x)) == 1을 추가했습니다.

which.second.max.na <- function(x, ...) 
    ifelse(length(x) == sum(is.na(x)) | sum(!is.na(x)) == 1, 0, 
     which.max(`[<-`(x, which.max(x), NA))) 

m2 <- calc(rs, which.second.max.na) 

우리는 which.max.nawhich.second.max.na가 작동하는지 확인하기 위해 처음 몇 값을 인쇄 할 수 있습니다.

head(values(m1)) 
[1] 2 1 4 2 1 2 

head(values(m2)) 
[1] 4 3 2 1 2 3 

head(values(rs)) 
     layer.1 layer.2 layer.3  layer.4 
[1,] 0.2875775 0.7999890 0.03872603 0.784575267 
[2,] 0.7883051 0.5328235 0.76235894 0.009429905 
[3,] 0.4089769 0.6886130 0.40136573 0.779065883 
[4,] 0.8830174 1.1544738 0.31502973 0.729390652 
[5,] 0.9404673 0.6829024 0.20257334 0.630131853 
[6,] 0.0455565 1.0903502 0.68024654 0.480910830 

RasterStack의 처음 6 개 값처럼 두 함수가 모두 예상대로 작동합니다.

마지막으로 RasterStack에 결속이있는 경우이 두 기능에 문제가있을 수 있습니다.

+0

완벽하게 작동합니다. 감사합니다. – Marc

관련 문제