2015-06-26 57 views
0

我是一種新的R所以也許它是一個愚蠢的問題,但我無法弄清楚自己。在R從多個光柵文件創建最大柵格

這是我的問題。 我有多個具有相同網格大小的asc文件並覆蓋相同的區域。我想從所有的asc文件中獲取每個網格的最大值。 我試過許多東西:

for (i in 1:144){ 
    asc0<-rasterToPoints(raster(asc0)) 
     asc1<-rasterToPoints(raster(asc[i])) 
     asc0[,3] <-pmax(asc0[,3], asc1[,3]) 
    } 

當我環扔了文件,becasue它留下了NA,所以我ASC0(我的基本文件)是不同的大小比我的下一個文件ASC1這一次失敗[2] 。

有沒有人知道一種方法呢?我有我的循環準備好通過所有的文件,有13x144文件。 但我不能找出一種方法來獲得最大值,存儲它,並將其與下一個文件進行比較。

謝謝,真的appriciate你的幫助!

回答

1

使用stackmax

r1 <- r2 <- r3 <- raster(nrows=10, ncols=10, xmn=0, xmx=10, ymn=0, ymx=10) 
r1[] <- 1 # raster with 1 
r2[] <- 2 # raster with 2 
r3[] <- 3 # raster with 3 
s1 <- stack(r1, r2, r3) 
s1 
#class  : RasterStack 
#dimensions : 10, 10, 100, 3 (nrow, ncol, ncell, nlayers) 
#resolution : 1, 1 (x, y) 
#extent  : 0, 10, 0, 10 (xmin, xmax, ymin, ymax) 
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#names  : layer.1, layer.2, layer.3 
#min values :  1,  2,  3 <- values between 1 and 3 
#max values :  1,  2,  3 <- values between 1 and 3 

max(s1) 
#class  : RasterLayer 
#dimensions : 10, 10, 100 (nrow, ncol, ncell) 
#resolution : 1, 1 (x, y) 
#extent  : 0, 10, 0, 10 (xmin, xmax, ymin, ymax) 
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
#data source : in memory 
#names  : layer 
#values  : 3, 3 (min, max) <- 3 only