Hi, There is an easy way to do this, but it is outside R. I use GME (geospatial modelling environment). It can also run R commands however.
The function in that program you are looking for is: countpntsinpolys More information on: http://www.spatialecology.com/gme/gmecommands.htm Best, Mieke >-----Original Message----- >From: r-sig-geo-boun...@r-project.org [mailto:r-sig-geo-bounces@r- >project.org] On Behalf Of Agustin Lobo >Sent: 18 October 2011 15:25 >To: r-sig-geo >Subject: [R-sig-Geo] Counting points within polygons > >Hi! > >Given a Sp Points DF and an Sp Polygons DF, I want to add a field of >number of points for each polygon >to the Sp Pol. DF table. This is what I'm doing (using objects from >the help page for sp::overlay() >(meuse is the Sp Points DF and srdf the Sp Pol. DF) > >> delme3 <- overlay(meuse,srdf) >> delme3 > [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >1 1 1 1 > [38] 1 1 1 1 1 1 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 >3 3 3 3 > [75] 3 3 3 3 3 3 3 3 2 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 >2 2 2 2 >[112] 2 2 2 2 2 2 3 2 2 2 2 2 1 2 2 2 2 1 1 2 2 1 2 2 2 2 3 3 3 3 3 3 3 >3 3 3 3 >[149] 3 3 3 3 3 3 3 >newdata <- data.frame(srdf@data,nppoints=table(delme3)) >srdf2 <- srdf >srdf2@data <- newdata > >An alternative: >newdatap <- data.frame(meuse@data, nbp=rep(1,nrow(meuse))) >meuse2 <- meuse >meuse2@data <- newdatap >delme4 <- overlay(meuse2[,13],srdf,fn=sum) >newdata <- data.frame(srdf@data,nppoints=delme4) >srdf2 <- srdf >srdf2@data <- newdata > >I wonder if there is a better way or, best, an standard R spatial >function doing that already. > >Thanks! > >Agus > >_______________________________________________ >R-sig-Geo mailing list >R-sig-Geo@r-project.org >https://stat.ethz.ch/mailman/listinfo/r-sig-geo _______________________________________________ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo