[R] flatten a list

Gabor Grothendieck ggrothendieck at gmail.com
Tue Sep 29 17:26:57 CEST 2015


> do.call(c, lapply(temp, function(x) if (is.list(x)) x else list(x)))
[[1]]
[1] 1 2 3

[[2]]
[1] "a" "b" "c"

$duh
[1] 5 6 7 8

$zed
[1] 15 16 17


On Tue, Sep 29, 2015 at 11:00 AM, Therneau, Terry M., Ph.D. <
therneau at mayo.edu> wrote:

> I'd like to flatten a list from 2 levels to 1 level.  This has to be easy,
> but is currently opaque to me.
>
> temp <- list(1:3, list(letters[1:3], duh= 5:8),  zed=15:17)
>
> Desired result would be a 4 element list.
> [[1]] 1:3
> [[2]] "a", "b", "c"
> [[duh]] 5:8
> [[zed]] 15:17
>
> (Preservation of the names is not important)
>
> Terry T
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list