2013-08-29 80 views
1

我有一個100柵格圖層的矩陣,我想創建一個平均的新圖層。我知道如果有兩層,我可以簡單地使用覆蓋功能,或者只是使用c <- mean (a, b)。但是,我不確定如何繼續矩陣。矩陣柵格覆蓋

這裏是矩陣的樣本:

[[1]] 
class  : RasterLayer 
dimensions : 175, 179, 31325 (nrow, ncol, ncell) 
resolution : 1, 1 (x, y) 
extent  : 0, 179, 0, 175 (xmin, xmax, ymin, ymax) 
coord. ref. : NA 
data source : in memory 
names  : layer 
values  : 0, 100 (min, max) 

我已經試過

a.avg <- mean (a.total[,]) 

,我收到錯誤argument is not numeric or logical: returning NA

+0

您是否有'柵格圖層矩陣'?或列表?或者更好的堆棧? –

+0

我在平均之前使用下面的建議創建了一個堆棧,它工作正常。謝謝。 – user2729279

回答

0

我假設你有rasterLayerlist A S(或者a stack)。如果你已經有了一個stack,跳過步驟之一,但我假設你有一個list不是matrix我稱之爲mylistofrasters ...

#1 - Get all rasters in the list into a stack 
mystack <- do.call(stack , mylistofrasters) 

#2 - Take mean of each pixel in the stack returning a single raster that is the average 
mean.stack <- calc(mystack , mean , na.rm = TRUE) 
0

這個答案是類似於使用簡單的代碼@ SimonO101的答案。

首先,讓我們構建的RasterLayer列表(你可以跳過這一步,如果你已經有了名單):

library(raster) 

r <- raster(nrow=10, ncol=10) 
r <- init(r, runif) 
lr <- lapply(1:8, function(i)r) 

raster包定義了列表的stack方法,這樣你就可以直接使用,而不do.call

s <- stack(lr) 

此外,存在用於Raster*對象mean方法。因此,您並不需要calc

mean(s, na.rm=TRUE)