Skip to content

is there a way to recursilvely lapply

6 messages · Gabor Grothendieck, Whit Armstrong, Brian Ripley

#
for a simple example:

x <- list()
x[["a"]] <- list(a=c(1,2,3),b=c(3,4,5))
x[["b"]] <- list(a=c(6,7,8),b=c(9,10,11))

lapply(x,sum)


this fails w/
Error in FUN(X[[1L]], ...) : invalid 'type' (list) of argument

Just wondering if I have overlooked something obvious.

one can also do:

lapply(x,lapply,sum)

but that assumes that you already know how many levels you have, and
that all the levels are consistent.

-Whit
#
This will recursively lapply and may or may not be
what you are looking for:

rapply(x, sum)

On Thu, Dec 11, 2008 at 2:59 PM, Whit Armstrong
<armstrong.whit at gmail.com> wrote:
#
On Thu, 11 Dec 2008, Whit Armstrong wrote:

            
rapply?  Which is linked from ?lapply (I just checked).  Perhaps
a.a a.b b.a b.b
   6  12  21  30

or
$a
$a$a
[1] 6
.....

  
    
#
Thanks, Gabor and Prof. Ripley.

Sorry for the oversight.

I grepped the lapply help for recursive prior to sending my question.

why does it appear as "*r*ecursive" in the help file? or is that just
a formating problem on my machine?

-Whit


On Thu, Dec 11, 2008 at 3:13 PM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
#
On Thu, 11 Dec 2008, Whit Armstrong wrote:

            
It is marked as bold: I presume you are reading text help?

  
    
#
yes, that is correct. I was looking in text mode.

ok, thanks for your help.

-Whit


On Thu, Dec 11, 2008 at 4:02 PM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote: