# split_by: Split, order and sort lists by their components. In purrr: Functional Programming Tools

## 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")) ```

### Example output

```[[1]]
[[1]]\$x
[1] 2

[[1]]\$y
[1] 1

[[2]]
[[2]]\$x
[1] 9

[[2]]\$y
[1] 2

[[3]]
[[3]]\$x
[1] 5

[[3]]\$y
[1] 3

[[4]]
[[4]]\$x
[1] 8

[[4]]\$y
[1] 4

[[5]]
[[5]]\$x
[1] 3

[[5]]\$y
[1] 5

[[6]]
[[6]]\$x
[1] 7

[[6]]\$y
[1] 6

[[7]]
[[7]]\$x
[1] 4

[[7]]\$y
[1] 7

[[8]]
[[8]]\$x
[1] 6

[[8]]\$y
[1] 8

[[9]]
[[9]]\$x
[1] 10

[[9]]\$y
[1] 9

[[10]]
[[10]]\$x
[1] 1

[[10]]\$y
[1] 10

[1] 10  1  5  7  3  8  6  4  2  9
[[1]]
[[1]]\$x
[1] 1

[[1]]\$y
[1] 10

[[2]]
[[2]]\$x
[1] 2

[[2]]\$y
[1] 1

[[3]]
[[3]]\$x
[1] 3

[[3]]\$y
[1] 5

[[4]]
[[4]]\$x
[1] 4

[[4]]\$y
[1] 7

[[5]]
[[5]]\$x
[1] 5

[[5]]\$y
[1] 3

[[6]]
[[6]]\$x
[1] 6

[[6]]\$y
[1] 8

[[7]]
[[7]]\$x
[1] 7

[[7]]\$y
[1] 6

[[8]]
[[8]]\$x
[1] 8

[[8]]\$y
[1] 4

[[9]]
[[9]]\$x
[1] 9

[[9]]\$y
[1] 2

[[10]]
[[10]]\$x
[1] 10

[[10]]\$y
[1] 9

List of 2
\$ 1:List of 2
..\$ :List of 2
.. ..\$ g: int 1
.. ..\$ y: num [1:5] 10 3 7 2 10
..\$ :List of 2
.. ..\$ g: int 1
.. ..\$ y: num [1:5] 10 9 1 3 2
\$ 2:List of 3
..\$ :List of 2
.. ..\$ g: int 2
.. ..\$ y: num [1:5] 9 9 8 8 9
..\$ :List of 2
.. ..\$ g: int 2
.. ..\$ y: num [1:5] 1 2 5 10 1
..\$ :List of 2
.. ..\$ g: int 2
.. ..\$ y: num [1:5] 9 8 7 6 8
\$`1`
\$`1`[[1]]
[1] 10  3  7  2 10

\$`1`[[2]]
[1] 10  9  1  3  2

\$`2`
\$`2`[[1]]
[1] 9 9 8 8 9

\$`2`[[2]]
[1]  1  2  5 10  1

\$`2`[[3]]
[1] 9 8 7 6 8
```

purrr documentation built on May 29, 2017, 1:45 p.m.

Search within the purrr package
Search all R packages, documentation and source code