Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df0812111203g4b189572x401b0ad3671c0b27@mail.gmail.com>
Date: 2008-12-11T20:03:23Z
From: Gabor Grothendieck
Subject: is there a way to recursilvely lapply
In-Reply-To: <8ec76080812111159n6c05869em861a8d111cee2372@mail.gmail.com>

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:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>