2013-07-23 26 views
5

में प्रजातियों की घटनाओं की गणना करना मेरे पास पूरे अमेरिका में एक प्रवासी पक्षी प्रजातियों के घटना डेटा के R में लगभग 500,000 अंक हैं।ग्रिड

मैं इन बिंदुओं पर एक ग्रिड ओवरले करने का प्रयास कर रहा हूं, और फिर प्रत्येक ग्रिड में घटनाओं की संख्या की गणना करता हूं। एक बार गणना की गई है, तो मैं उन्हें एक ग्रिड सेल आईडी में संदर्भित करना चाहता हूं।

आर में, मैंने 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

@ हांगोई मुझे विश्वास है कि यह 'sp' है। – agstudy

+3

यह आपको प्रारंभ हो सकता है: [आर का उपयोग करके ग्रिड को अंक एकत्रित करना] (http://gis.stackexchange.com/a/48434/9803) – Ben

उत्तर

3

सबसे पहले, आपका आर कोड लिखित के रूप में काम नहीं करता है। मैं इसे एक साफ सत्र में कॉपी-पेस्ट करने का सुझाव दूंगा, और यदि यह आपके लिए भी त्रुटिपूर्ण है, तो सिंटैक्स त्रुटियों को ठीक करना या ऐड-ऑन लाइब्रेरीज़ को तब तक चलाएं जब तक यह चलता न हो।

उसने कहा, मुझे लगता है कि आपको दो आयामी संख्यात्मक निर्देशांक के 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);