# [R] flatten a list of lists

Michael Friendly friendly at yorku.ca
Thu Oct 17 16:42:06 CEST 2013

```Thanks to all who replied.

Here are two versions of a function (sans sanity checks) that do what I
want:

foo1 <- multifoo(1:2, "A")
foo2 <- multifoo(1:2, "B")

mfoo <- list(A=foo1, B=foo2)
class(mfoo) <- c("foolist", "list")

#' flatten a list of lists

# from Duncan Murdoch
flatten <- function(list, unname=TRUE) {
res <- do.call(c, if(unname) unname(list) else list)
class(res) <- class(list)
res
}

# from David Carlson
flatten2 <- function(list, unname=TRUE) {
res <- unlist(list, recursive = FALSE)
if(unname) names(res) <- unlist(lapply(list, names))
class(res) <- class(list)
res
}

mflat1 <- flatten(mfoo)
mflat2 <- flatten2(mfoo)
all.equal(mflat1,mflat2)

> all.equal(mflat1,mflat2)
[1] TRUE

-Michael

On 10/17/2013 9:39 AM, David Carlson wrote:
> Does this get you the rest of the way?
>
>> mfoo2 <- unlist(mfoo, recursive = FALSE)
>> names(mfoo2) <- unlist(lapply(mfoo, names))
>> class(mfoo2) <- "foolist"
>> str(mfoo2)
> List of 4
>   \$ A1:List of 2
>    ..\$ x: int 3
>    ..\$ y: int 10
>    ..- attr(*, "class")= chr "foo"
>   \$ A2:List of 2
>    ..\$ x: int [1:2] 6 4
>    ..\$ y: int [1:2] 8 9
>    ..- attr(*, "class")= chr "foo"
>   \$ B1:List of 2
>    ..\$ x: int 2
>    ..\$ y: int 2
>    ..- attr(*, "class")= chr "foo"
>   \$ B2:List of 2
>    ..\$ x: int [1:2] 3 6
>    ..\$ y: int [1:2] 4 2
>    ..- attr(*, "class")= chr "foo"
>   - attr(*, "class")= chr "foolist"
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
>
>
>
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ista Zahn
> Sent: Thursday, October 17, 2013 8:23 AM
> To: Michael Friendly
> Cc: R-help
> Subject: Re: [R] flatten a list of lists
>
> unlist(mfoo, recursive = FALSE) gets you pretty close.
>
> Best,
> Ista
>
> On Thu, Oct 17, 2013 at 9:15 AM, Michael Friendly
> <friendly at yorku.ca> wrote:
>> I have functions that generate lists objects of class "foo"
> and lists of
>> lists of these, of class
>> "foolist", similar to what is shown below.
>>
>> How can I flatten something like this to remove the top-level
> list
>> structure, i.e.,
>> return a single-level list of "foo" objects, of class
> "foolist"?
>> foo <- function(n) {
>>      result <- list(x=sample(1:10,n), y=sample(1:10,n))
>>      class(result) <- "foo"
>>      result
>> }
>>
>> multifoo <- function(vec, label, ...) {
>>      result <- lapply(vec, foo, ...)
>>      names(result) <- paste0(label, vec)
>>      class(result) <- "foolist"
>>      result
>> }
>>
>> foo1 <- multifoo(1:2, "A")
>> foo2 <- multifoo(1:2, "B")
>>
>> mfoo <- list(A=foo1, B=foo2)
>>
>> str(mfoo, 2)
>>
>>> str(mfoo, 2)
>> List of 2
>>   \$ A:List of 2
>>    ..\$ A1:List of 2
>>    .. ..- attr(*, "class")= chr "foo"
>>    ..\$ A2:List of 2
>>    .. ..- attr(*, "class")= chr "foo"
>>    ..- attr(*, "class")= chr "foolist"
>>   \$ B:List of 2
>>    ..\$ B1:List of 2
>>    .. ..- attr(*, "class")= chr "foo"
>>    ..\$ B2:List of 2
>>    .. ..- attr(*, "class")= chr "foo"
>>    ..- attr(*, "class")= chr "foolist"
>>
>> In this case, what is wanted is a single-level list, of 4 foo
> objects, A1,
>> A2, B1, B2,
>> all of class "foolist"
>>
>> --
>> Michael Friendly     Email: friendly AT yorku DOT ca
>> Professor, Psychology Dept. & Chair, Quantitative Methods
>> York University      Voice: 416 736-2100 x66249 Fax: 416
> 736-5814
>> 4700 Keele Street    Web:   http://www.datavis.ca
>> Toronto, ONT  M3J 1P3 CANADA
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible
> code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible
> code.
>

--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca