R: Flatten a Nested List of Vectors Into a Single Tier List of...

flatten {textshape} | R Documentation

## Flatten a Nested List of Vectors Into a Single Tier List of Vectors

### Description

Flatten a named, nested list of atomic vectors to a single level using the concatenated list/atomic vector names as the names of the single tiered list.

### Usage

    flatten(x, sep = "_", ...)

### Arguments

`x` | A nested, named list of vectors.
`sep` | A separator to use for the concatenation of the names from the nested list.
`...` | ignored.

### Value

Returns a flattened list.

### Note

The order of the list is sorted alphabetically. Pull requests for the option to return the original order would be appreciated.

### Author(s)

StackOverflow user @Michael and Paul Foster and Tyler Rinker <tyler.rinker@gmail.com>.

### References

https://stackoverflow.com/a/41882883/1000343
https://stackoverflow.com/a/48357114/1000343

### Examples

    x <- list(
        urban = list(
            cars = c('volvo', 'ford'),
            food.dining = list(
                local.business = c('carls'),
                chain.business = c('dennys', 'panera')
            )
        ),
        rural = list(
            land.use = list(
                farming =list(
                    dairy = c('cows'),
                    vegie.plan = c('carrots')
                )
            ),
            social.rec = list(
                community.center = c('town.square')
            ),
            people.type = c('good', 'bad', 'in.between')
        ),
        other.locales = c('suburban'),
        missing = list(
            unknown = c(),
            known = c()
        ),
        end = c('wow')
    )

    x

    flatten(x)
    flatten(x, ' -> ')

    ## Not run:
    ## dictionary from quanteda
    require(quanteda); library(textreadr); library(dplyr)
    mydict <- https://provalisresearch.com/Download/LaverGarry.zip" %>%
        textreadr::download(") %>%
        unzip(exdir = tempdir()) %>%
        `[`(1) %>%
        quanteda::dictionary(file = .)

    mydict
    flatten(as.list(mydict))

    ## End(Not run)

[Package textshape version 1.6.0 Index]