Learn R Programming

purrr (version 0.1.0)

split_by: Split, order and sort lists by their components.

Description

Split, order and sort lists by their components.

Usage

split_by(.x, .f, ...)

order_by(.x, .f, ...)

sort_by(.x, .f, ...)

Arguments

.x
A list or vector.
.f
A function, formula or string.

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function with a three arguments, .x or ., .y, .z. This allows you

...
Additional arguments passed on to .f.

Examples

Run this code
l1 <- zip_n(list(x = sample(10), y = 1:10))
l1
l1 %>% order_by("x")
l1 %>% sort_by("x")

l2 <- rerun(5, g = sample(2, 1), y = rdunif(5, 10))
l2 %>% split_by("g") %>% str()
l2 %>% split_by("g") %>% map(. %>% map("y"))

Run the code above in your browser using DataLab