[R-sig-Geo] sum number of events by covariate using applynbd

Wall, Wade A ERDC-RDE-CERL-IL Wade.A.Wall at usace.army.mil
Mon May 14 16:27:33 CEST 2012


Thanks,

That worked perfectly. And sorry for my sloppy use of language.

Have a great day,

Wade

-----Original Message-----
From: Rolf Turner [mailto:r.turner at auckland.ac.nz] 
Sent: Friday, May 11, 2012 8:24 PM
To: Wall, Wade A ERDC-RDE-CERL-IL
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] sum number of events by covariate using applynbd



PLEASE!!!  Do NOT confuse "covariate" with "mark".  They are very different concepts.  You want to "segregate by ***mark***" (etc.) NOT by covariate.  The latter makes no sense at all.

As to how to accomplish the desired task, see below.

On 12/05/12 09:23, Wall, Wade A ERDC-RDE-CERL-IL wrote:
> Hi all,
>
> I am wanting to sum the number of events within r=5 distance of a set of points and segregate by a covariate. I want to use the function applynbd, but not sure how to write a custom function to do this.
>
> For example, I have a ppp object, LS.ppp. LS.ppp$marks has two levels, M and F.
>
> For each point in the data set, I want to assess the sum of all points within r=5 that are M and the sum of all points that are female.
>
> The results would be a dataframe  of N rows, with N = the number of points in the ppp object.
>
> The two columns would be number of females, and number of males.
>
> I think this could be done using dist() and looping over it for each row and then matching with a dataframe, but that seems to be a clunky way of doing it.
>
> Does anyone know how to do this? I see how to use applynbd to get the sum of the points, but not the sum by covariate.
>
> Thanks for any information.

This is actually fairly easy to do with applynbd() but is even easier to do using markstat():

DF <- as.data.frame(t(markstat(LS.ppp,fun=table,R=5,exclude=TRUE)))

If you *really* want to use applynbd() directly ("just because you can") then do:

foo <- function(Y,current,dists,dranks,...){table(marks(Y))}
DF <- as.data.frame(t(applynbd(LS.ppp,foo,R=5,exclude=TRUE)))

Note:  I'm using "exclude=TRUE" because I presume that you do not want to count the point itself when tabulating the marks of points within distance 5 of that point.  If you *do* want to count the point itself then remove reference to "exclude" or set exclude=FALSE (the default).

     cheers,

         Rolf Turner



More information about the R-sig-Geo mailing list