2013-07-23 8 views
5

米国全体で渡り鳥の発生データのRに約50万ポイントあります。グリッド内の種の数をカウントする

これらのポイントにグリッドをオーバーレイし、各グリッド内のオカレンスの数を数えようとしています。カウントが集計されたら、それらをグリッド・セルIDに参照する必要があります。

Rでは、over()関数を使用して、範囲マップ内の点(シェイプファイル)を取得しました。

#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の2次元数値座標で終わると仮定します。だから、ビンニングとカウントの目的のために、そのようなデータがあれば、私はそのようなデータセットをシミュレートする自由を取った。これがあなたのデータの関連する側面を捕らえない場合は、私を修正してください。

## 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); 
関連する問題