4

I'm trying to create a list of point patterns ppp.object {spatstat} in a loop. My dataset looks like this:

> names(OT1);head(OT1);dim(OT1)
[1] "EID"       "latitude"  "longitude" "month"     "year"      "CPUE"      "TSUM"     
[8] "fTSUM"    
                EID latitude longitude month year CPUE TSUM fTSUM
1   167-1-1996-1135 67.70000 -61.81667     9 1996    0    0     F
2  167-10-1996-1135 67.71667 -59.18333     9 1996    0    0     F
3 167-100-1996-1135 67.86667 -59.43333    10 1996    0    0     F
4 167-101-1996-1135 67.95000 -59.58333    10 1996    0    0     F
5 167-102-1996-1135 68.10000 -59.76667    10 1996    0    0     F
6 167-103-1996-1135 67.81667 -59.38333    10 1996    0    0     F
[1] 2707    8

What I would like to do is to select data for each of my month and create a ppp.object.

> sort(unique(OT1$month))
[1]  7  8  9 10 11 12

The following loop works and I can see each of my figures:

for(i in sort(unique(OT1$month))){ 
  a<-OT1[OT1$month==i,]
  b<-ppp(a$longitude,a$latitude,marks=a$fTSUM,window=newW)
  plot(b,main=i)
}

I would like to create a list of all my ppp.object that I can access individually, I've tried adding a list() in the loop command but without any success... Any help would be much appreciated!

Thank you!

6
  • Please do not crosspost between r-help and here. Bad karma. Commented Mar 9, 2012 at 15:53
  • 2
    @DirkEddelbuettel - Why not? Is everyone who frequents the [R] tag here also a follower of R-help? It seems like two completely distinct communities to me. Commented Mar 9, 2012 at 16:15
  • @eykanal I'd be willing to bet that nearly every R tag regular on SO at least lurks on R-help. Commented Mar 9, 2012 at 16:18
  • 1
    Yes, but not everyone who is looking for assistance with R will search both here and R-help. I'm not sure I agree with the suggested restriction. Commented Mar 9, 2012 at 16:21
  • 3
    @eykanal For me, it's more simply about politeness. Asking for help here or on R-help is essentially asking for busy folks to take time out and help you for free. If the consensus among those communities is that cross-posting is discourage (and it is, given by their FAQs) then it's just polite to respect that. Commented Mar 9, 2012 at 16:25

3 Answers 3

9

This seems like a good fit for lapply:

pppList <- lapply(sort(unique(OT1$month)), function(i) {
  a<-OT1[OT1$month==i,]
  ppp(a$longitude,a$latitude,marks=a$fTSUM,window=newW)
})

...But just to explain how to fix you for-loop:

You need to have a list to assign to. And creating one of the correct length is always a good idea for performance:

x <- sort(unique(OT1$month))
pppList <- vector('list', length(x))
for(i in x) { 
  a<-OT1[OT1$month==i,]
  b<-ppp(a$longitude,a$latitude,marks=a$fTSUM,window=newW)
  pppList[[i]] <- b
  plot(b,main=i)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Tommy! I was struggling on how to code the list function! This works perfectly.
2

Would the following work for you?

library(plyr)
dlply(OT1, .(month), function(a) ppp(a$longitude,a$latitude,marks=a$fTSUM))

Comments

1

You should create an empty list of the correct length first, and then assign each ppp object to a slot in that list in turn.

ind <- sort(unique(OT1$month))
b <- vector("list", length(ind))
for(i in 1:length(ind)){ 
    a <- OT1[OT1$month == ind[i],]
    b[[i]] <- ppp(a$longitude,a$latitude,marks=a$fTSUM,window=newW)
    plot(b[[i]],main=ind[i])
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.