Maiasaura
Maiasaura

Reputation: 32986

passing a list argument to llply

I've never found a satisfactory answer to this question. Assuming I have a function:

foo <- function(a,b,c) {
...
}

a is a list containing many data.frame objects

b is a list containing a special class of data (class ppp from library spatstat if that helps any).

c is an integer.

and I want to push a list through that function:

result <- llply(a, foo, b,c) 

The names of list items in b correspond to the column "type" in the data.frames in list a. So inside function foo I can quickly do:

id <- which(names(b) == unique(a$type))
baseline_pattern <- b[[id]]

to match each underlying spatial pattern in b with the right data.frame in a. There are about 10 items in b and 1000s of items in a. Unfortunately this doesn't work because llply (or lapply in general) tries to push one list item at a time, even for argument list b and the two lines of code matching the correct list item in b with the correct data frame fails because the function no longer has the entire list b. Is there any way around this?

In a pinch, I could create another list (effectively combining a and b) where each list item is itself a list with length 2 containing the data.frame and the corresponding point pattern object. But this will result in 1000s of redundant copies (and make running this operation on a cluster far less efficient).

UPDATE: I plan to turn this into a mclapply to run this on a cluster. I cannot do that with mapply (unless there is a mcmapply).

Upvotes: 1

Views: 623

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57686

I haven't woken up completely yet, but it sounds like you want to use mapply with MoreArgs=list(b, c).

Upvotes: 1

Related Questions