# [R-sig-Geo] Need Help With Loop

Rolf Turner r.turner at auckland.ac.nz
Wed Apr 17 09:44:17 CEST 2013

```
I am not at all sure that I understand your question,   but I *think*
that you do not need to use loops at all.  Let me assume that
you have vectors of point coordinates x and y, a vector of
months associated with the points, sat "m" (which is a *factor* of the
same length as x and y) and an observation window (say W)
within which the points lie.

You can do something like:

# Generate some toy data.
x <- runif(1000,0,10)
y <- runif(1000,0,10)
m <- factor(sample(month.abb,1000,TRUE))
W <- owin(c(0,10),c(0,10))

# Now do the processing of the data.
X <- ppp(x,y,window=W,marks=m)
SX <- split(X)
foo <- function(x,sigma,k.keep) {
iii <- density(x,sigma=sigma,diggle=TRUE)
jjj <- as.owin(iii[eval.im(iii>=k.keep*max(iii)),drop=FALSE])
convexhull(jjj)
}
RSLT <- lapply(SX,foo,sigma=0.5,k.keep=0.5)

Does that get you where you wanted to go?

cheers,

Rolf Turner

On 17/04/13 05:19, Reeder, Bryce Wesley wrote:
> Dear List,
>
> This is likely a silly question, but I am just beginning to learn how to
> incorporate loops into my functions.  I have written a function that allows
> me to perform a series of steps that I need to do:
>
> function.x <- function(x, y, window, b.size, k.keep){
>    ppp <- ppp(x, y, window=window)
>    k <- density(ppp, b.size, edge=TRUE, at="pixel", diggle=TRUE,
> window=window)
>    df <- data.frame(k)
>    k.extract <- df[df\$value >= (k.keep*max(df\$value)), ]
>    extract.ppp <- ppp(k.extract\$x, k.extract\$y, window=window)
>    shape <- convexhull(extract.ppp)
> }
>
> As of now, I must subset the my data first into months and then run the
> function for each month separately.  I have been trying all morning to
> insert a loop that can subset my data into months for me, run the function,
> and then return the convexhulls as objects that I can save as polygons.

```