6

R 私は、米国全土の渡り鳥種の出現データを約 500,000 ポイント持っています。

これらのポイントにグリッドを重ねて、各グリッドの出現回数を数えようとしています。カウントが集計されたら、それらをグリッド セル 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")))

上記は私が望んでいたとおりに機能しましたが、現在の問題に対処していません: typeSpatialPointsDataFrameであるポイントとラスターであるグリッドを処理する方法。ラスター グリッドをポリゴン化し、上記と同じ方法を使用することをお勧めしますか? それとも、別のプロセスの方が効率的ですか?

4

1 に答える 1

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);
于 2013-07-24T17:34:01.503 に答える