2013-07-23 53 views
5

我在美國各地的候鳥種類的發生數據中有大約500,000點R計算網格中的物種發生

我試圖覆蓋這些點上的網格,然後計算每個網格中發生的次數。一旦統計完成後,我想引用它們到網格單元ID。

在R中,我使用over()函數來獲取範圍映射中的點,這是一個shapefile。

#Read in occurrence data 
data=read.csv("data.csv", header=TRUE) 
coordinates(data)=c("LONGITUDE","LATITUDE") 

#Get shapefile of the species' range map 
range=readOGR(".",layer="data") 

proj4string(data)=proj4string(range) 

#Get points within the range map 
inside.range=!is.na(over(data,as(range,"SpatialPolygons"))) 

以上工作正是我希望的,但並沒有解決我目前的問題:如何處理那些SpatialPointsDataFrame的類型和網格光柵點。你會推薦多邊形化柵格網格,並使用我上面指出的相同方法?或者另一個過程會更有效率?

+0

您正在使用哪個軟件包? –

+0

@HongOoi我相信它是'sp'。 – agstudy

+3

這可能會讓你開始:[使用R將點聚合到網格](http://gis.stackexchange.com/a/48434/9803) – Ben

回答

3

首先,您的R代碼不能像寫入一樣工作。我建議將它複製粘貼到一個乾淨的會話中,並且如果它也出錯了,請更正語法錯誤或包括附加庫直到它運行。

這就是說,我假設你應該以二維數字座標的data.frame結尾。所以,爲了對它們進行分箱和計數,任何這樣的數據都可以,因此我冒昧地模擬了這樣的數據集。如果這不能捕獲數據的相關方面,請糾正我。

## Skip this line if you are the OP, and substitute the real data instead. 
data<-data.frame(LATITUDE=runif(100,1,100),LONGITUDE=runif(100,1,100)); 

## Add the latitudes and longitudes between which each observation is located 
## You can substitute any number of breaks you want. Or, a vector of fixed cutpoints 
## LATgrid and LONgrid are going to be factors. With ugly level names. 
data$LATgrid<-cut(data$LATITUDE,breaks=10,include.lowest=T); 
data$LONgrid<-cut(data$LONGITUDE,breaks=10,include.lowest=T); 

## Create a single factor that gives the lat,long of each observation. 
data$IDgrid<-with(data,interaction(LATgrid,LONgrid)); 

## Now, create another factor based on the above one, with shorter IDs and no empty levels 
data$IDNgrid<-factor(data$IDgrid); 
levels(data$IDNgrid)<-seq_along(levels(data$IDNgrid)); 

## If you want total grid-cell count repeated for each observation falling into that grid cell, do this: 
data$count<- ave(data$LATITUDE,data$IDNgrid,FUN=length); 
## You could have also used data$LONGITUDE, doesn't matter in this case 

## If you want just a table of counts at each grid-cell, do this: 
aggregate(data$LATITUDE,data[,c('LATgrid','LONgrid','IDNgrid')],FUN=length); 
## I included the LATgrid and LONgrid vectors so there would be some 
## sort of descriptive reference accompanying the anonymous numbers in IDNgrid, 
## but only IDNgrid is actually necessary 

## If you want a really minimalist table, you could do this: 
table(data$IDNgrid);