Learn R Programming

purrr (version 0.1.0)

flatten: Flatten a list of lists into a list.

Description

This is a wrapper for unlist() that defaults to recursive = FALSE.

Usage

flatten(.x, .recursive = FALSE, .unname = FALSE)

Arguments

.x
A list of flatten
.recursive
Recursively flatten list components?
.unname
Remove names before flattening?

Details

When .unname is TRUE and .recursive is FALSE, only the names of .x are removed, leaving the inner lists intact. But if .recursive is TRUE, the names of all inner lists are also removed.

Examples

Run this code
x <- rerun(10, sample(5))
x %>% flatten(x)

# You can use flatten in conjunction with map
x %>% map(~ .x[1]) %>% flatten()
# But it's better to use map_int etc.
x %>% map_int(~ .x[1])

Run the code above in your browser using DataLab