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

Description Usage Arguments Value Note Author(s) References Examples

View source: R/flatten.R

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

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)

textshape documentation built on May 29, 2021, 1:07 a.m.