[R] apply over parallel lists and their elements

Liviu Andronic landronimirc at gmail.com
Tue Sep 14 08:47:06 CEST 2010


On Tue, Sep 14, 2010 at 12:44 AM, David Winsemius
<dwinsemius at comcast.net> wrote:
> The second argument to mean is trim. I am not sure what mean(1, 3) is
> supposed to do but what it return is 1.
>
Thanks for the info. On this particular point I find the documentation
confusing. In ?mapply :
'‘mapply’ applies
     ‘FUN’ to the first elements of each ...  argument, '

mapply(FUN, ..., MoreArgs = NULL, [..]

'     ...: arguments to vectorize over (list or vector).'

In my understanding this suggests that '...' can take several comma
separated objects, so that in
mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values)

the second object should not be treated as a 'MoreArgs' argument. But
I'm probably wrong.


> If you wanted 2,3,4 ..., 11 then you
> would perhaps do:
>
> mean( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values) )
>
I think the original poster was more interested in finding the mean()
by rows. Instead of
> mean( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values) )
[1] 6.5

he probably looks for
> apply( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values), 2, mean )
 [1]  2  3  4  5  6  7  8  9 10 11

although I'm positive there is a neater way to do this. For example,
apply( data.frame(tree[[1]]$node$values, tree[[2]]$node$values), 1, mean )

Assembling your data in a data.frame prior to using an *pply function
would eliminate the need to write them all by
hand. Regards
Liviu



More information about the R-help mailing list