Learn R Programming

purrr (version 0.1.0)

zip_n: Zip lists.

Description

Zip turns a list-of-lists "inside-out". For example, if you had a list of length n where each component had values a and b, zip_n() would make a list with elements a and b that contained lists of length n.

Usage

zip_n(.x, .fields = NULL, .simplify = FALSE)

zip2(.x, .y, .fields = NULL, .simplify = FALSE)

zip3(.x, .y, .z, .fields = NULL, .simplify = FALSE)

Arguments

.x,.y,.z
Lists or vectors
.fields
Fields to use when unzipping - defaults to the names of the first sub-list.
.simplify
If TRUE, lists containing atomic scalars of the same type will be converted to a vector.

Details

Note that zip_n() is its own inverse, much like the transpose operation on a matrix. You can get back the original input by zipping it twice. zip2(x, y) is a shortcut for zip_n(list(x, y)).

Examples

Run this code
x <- rerun(5, x = runif(1), y = runif(5))
x %>% str()
x %>% zip_n() %>% str()
x %>% zip_n() %>% zip_n() %>% str()

x <- list(a = 1:5, b = 5:1)
x %>% zip_n()
x %>% zip_n(.simplify = TRUE)

# Zipping is its own inverse operation
x %>% str()
x %>% zip_n(.simplify = TRUE) %>% zip_n(.simplify = TRUE) %>% str()

# zip2(x, y) is a shortcut for zip_n(list(x, y)). It can be handy
# but doesn't handle names and is not its own inverse.
x %>% zip_n() %>% str()
zip2(1:5, 5:1) %>% str()

Run the code above in your browser using DataLab