[R-sig-Geo] How to count unique TRUE's
Walter Anderson
wandrson01 at gmail.com
Tue Mar 31 19:55:18 CEST 2015
I have a buffered project SpatialPolygonDataFrame and a
SpatialPointDataFrame that I want to intersect (one project at a time)
and count the number of points within each polygon's footprint.
I am using the following code:
for (i in 1:numprjs)
{
curprj <- as.character(output at PROJECT[i])
tmp <- subset(buf2640, buf2640$PROJECT == curprj)
tmp2 <- as.vector(gIntersects(tmp, ac, byid=TRUE))
output$AC[i] <- length(tmp[tmp2 == TRUE])
}
The problem is that when the project's polygons cover more than one
overlapping shapes (derived from multiple sub-projects that are given
the same project id), the above code can multiple count a single ac
point if it falls within the boundaries of two or more of a single
projects polygons.
As an example project 'B1103' has two points within its two polygons;
however, the above code reports three. I believe the following extract
of the core gIntersects command shows why
2 3
122 FALSE TRUE
313 TRUE TRUE
So clearly the problem is my use of as.vector, which is taking all of
the columns and combining them into a single vector; however, I am
unsure of the best way to count the number of true in the original data
structure created by the gIntersects command.
Walter Anderson
More information about the R-sig-Geo
mailing list