[R] how to flatten a list to the same level?

Henrique Dallazuanna wwwhsd at gmail.com
Fri Jan 8 18:29:36 CET 2010


Try something about like this:

split(unlist(l), rep(1:length(idx <- rapply(l, length)), idx))

On Fri, Jan 8, 2010 at 1:35 PM, Mark Heckmann <mark.heckmann at gmx.de> wrote:
> I have a nested list l like:
>
> l <- list(A=c(1,2,3), B=c("a", "b"))
> l <- list(l,l, list(l,l))
>
> I want the list to be unlisted, but not on the lowest level of each
> "branch".
> I want the lowest level of each list branch to remain as it is.
> So unlist or unlist(rec=F) do not work here as the level of nesting may
> differ on the elements.
> The result should look like:
>
> $A
> [1] 1 2 3
>
> $B
> [1] "a" "b"
>
> $A
> [1] 1 2 3
>
> $B
> [1] "a" "b"
>
> $A
> [1] 1 2 3
>
> $B
> [1] "a" "b"
>
> $A
> [1] 1 2 3
>
> $B
> [1] "a" "b"
>
> Any ideas?
> TIA!
>
> Mark
>
>
> –––––––––––––––––––––––––––––––––––––––
> Mark Heckmann
> Dipl. Wirt.-Ing. cand. Psych.
> Vorstraße 93 B01
> 28359 Bremen
> Blog: www.markheckmann.de
> R-Blog: http://ryouready.wordpress.com
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list