Skip to content
Prev 256414 / 398506 Next

For->lapply->parallel apply

Hi,
On Sat, Apr 9, 2011 at 5:03 AM, Alaios <alaios at yahoo.com> wrote:
Your loop can be converted quite easily.

The lapply function simply takes an object to iterate over as its
first argument (this can be a list of things, a vector of things,
etc.) and a function to apply to each element in the iteration.
`lapply` will build a list of results that your function returns for
each element.

A simple example is to iterate over the words in a character vector
and return how many characters are in each word.

R> words <- c('cat', 'dog's, 'people')
R> sizes <- lapply(words, function(x) nchar(x))
R> sizes
[[1]]
[1] 3

[[2]]
[1] 4

[[3]]
[1] 6

So in your example:
Could be something like:

gauslist <- lapply(1:dimz, function(i) {
  GaussRF(x=x, y=y, model=model, ... WHATEVER ELSE)
})

using mclapply would be exactly the same, except replace lapply with mclapply.

Actually, is it correct that you aren't doing anything different in
the iterations of the for loop -- I mean, nothing in your code really
depends on your value for `i`, right?