Split, order and sort lists by their components.

Share:

Description

Split, order and sort lists by their components.

Usage

1
2
3
4
5
split_by(.x, .f, ...)

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

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

Arguments

.x

A list or atomic vector.

.f

A function, formula, or atomic vector.

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function with two arguments, .x or . and .y. This allows you to create very compact anonymous functions with up to two inputs.

If character or integer vector, e.g. "y", it is converted to an extractor function, function(x) x[["y"]]. To index deeply into a nested list, use multiple values; c("x", "y") is equivalent to z[["x"]][["y"]]. You can also set .null to set a default to use instead of NULL for absent components.

...

Additional arguments passed on to .f.

Examples

1
2
3
4
5
6
7
8
l1 <- transpose(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"))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.