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

Forrest Stevens forrest at ufl.edu
Tue Mar 31 20:56:13 CEST 2015


I'm guessing based on the data and situation you describe that some
variant of this would probably get you close:


##  Sample data:
d <- data.frame(A=c(T,F,F,T), B=c(F,T,F,T))

##  Count row-wise trues:
sum( apply(d, MARGIN=1, sum) >= 1 )


Hope that helps,
Forrest
--
Forrest R. Stevens
Ph.D. Candidate, QSE3 IGERT Fellow
Department of Geography
Land Use and Environmental Change Institute
University of Florida
www.clas.ufl.edu/users/forrest


On Tue, Mar 31, 2015 at 2:29 PM, Tom Philippi <tephilippi at gmail.com> wrote:
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list