[R-sig-Geo] How to count unique TRUE's

Tom Philippi tephilippi at gmail.com
Tue Mar 31 20:29:59 CEST 2015


I can't quite make sense of what your objects look like, but I suspect you
need to change byid to a vector, so you are byid for the points but not the
polygons byid=c(FALSE,TRUE) or vice versa.


On Tue, Mar 31, 2015 at 10:55 AM, Walter Anderson <wandrson01 at gmail.com>
wrote:

> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list