flatten {purrr}R Documentation

Flatten a list of lists into a simple vector.

Description

These functions remove a level hierarchy from a list. They are similar to unlist(), but they only ever remove a single layer of hierarchy and they are type-stable, so you always know what the type of the output is.

Usage

flatten(.x)

flatten_lgl(.x)

flatten_int(.x)

flatten_dbl(.x)

flatten_chr(.x)

flatten_raw(.x)

flatten_dfr(.x, .id = NULL)

flatten_dfc(.x)

Arguments

.x

A list to flatten. The contents of the list can be anything for flatten() (as a list is returned), but the contents must match the type for the other functions.

.id

Either a string or NULL. If a string, the output will contain a variable with that name, storing either the name (if .x is named) or the index (if .x is unnamed) of the input. If NULL, the default, no variable will be created.

Only applies to ⁠_dfr⁠ variant.

Value

flatten() returns a list, flatten_lgl() a logical vector, flatten_int() an integer vector, flatten_dbl() a double vector, and flatten_chr() a character vector.

flatten_dfr() and flatten_dfc() return data frames created by row-binding and column-binding respectively. They require dplyr to be installed.

Examples

Run examples

x <- rerun(2, sample(4))
x
x %>% flatten()
x %>% flatten_int()

# You can use flatten in conjunction with map
x %>% map(1L) %>% flatten_int()
# But it's more efficient to use the typed map instead.
x %>% map_int(1L)

[Package purrr version 0.3.4 Index]