2013-07-23 8 views
5

미국 전역에서 철새 종의 발생 데이터가 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"))) 

는 위의 I 희망대로 정확하게 일을하지만, 내 현재의 문제를 해결하지 않습니다 어떻게 유형 SpatialPointsDataFrame 및 래스터입니다 그리드입니다 점에 대처하는 방법을. 래스터 그리드를 폴리곤 화하고 위의 동일한 방법을 사용할 것을 권장합니까? 아니면 다른 프로세스가 더 효율적입니까?

+0

어떤 패키지를 사용하고 있습니까? –

+0

@HongOoi 나는 그것이'sp'라고 믿는다. – agstudy

+3

다음과 같이 시작할 수 있습니다 : [R을 사용하여 그리드에 포인트 집계하기] (http://gis.stackexchange.com/a/48434/9803) – Ben

답변

3

우선 R 코드가 작성된대로 작동하지 않습니다. 깨끗한 세션에 복사하여 붙여 넣는 것이 좋습니다. 오류가 발생하면 구문 오류를 수정하거나 실행될 때까지 추가 기능 라이브러리를 포함시켜야합니다.

즉, 나는 당신이 2 차원 숫자 좌표 data.frame으로 끝내기로되어 있다고 가정합니다. 따라서 비닝 (binning)과 카운트 (counting)를 위해 이러한 데이터가 수행되므로 이러한 데이터 집합을 시뮬레이션 할 자유를 얻었습니다. 이것이 데이터의 관련성을 포착하지 않으면 저를 시정하십시오.

## 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);