Skip to contents

Arranges a vector x by the order of another vector by.

Usage

arrange_by(x, by)

Arguments

x

Vector to be ordered.

by

Reference vector which x will be ordered by.

Value

A permutation of x.

Details

Note that this function significantly differs from base::sort_by().

Examples

library(magrittr)

# generate 100 random letters
random_letters <-
  letters |>
  magrittr::extract(sample.int(n = 26L,
                               size = 100L,
                               replace = TRUE)) %T>%
  print()
#>   [1] "m" "w" "l" "e" "f" "o" "o" "d" "f" "i" "e" "e" "x" "o" "m" "b" "w" "k" "k" "e" "u" "l" "b" "f" "d" "p" "b" "c" "y" "v" "k" "g" "u" "f" "i" "v" "j" "u"
#>  [39] "b" "q" "t" "x" "v" "a" "f" "b" "s" "n" "a" "y" "o" "r" "e" "h" "a" "l" "s" "p" "k" "z" "c" "b" "w" "j" "c" "f" "d" "j" "a" "g" "f" "v" "s" "y" "b" "h"
#>  [77] "a" "a" "g" "l" "v" "i" "j" "b" "f" "g" "l" "z" "v" "n" "e" "g" "s" "l" "q" "e" "q" "m" "f" "a"

# sort the random letters alphabetically
random_letters |> pal::arrange_by(by = letters)
#>   [1] "a" "a" "a" "a" "a" "a" "a" "b" "b" "b" "b" "b" "b" "b" "b" "c" "c" "c" "d" "d" "d" "e" "e" "e" "e" "e" "e" "e" "f" "f" "f" "f" "f" "f" "f" "f" "f" "g"
#>  [39] "g" "g" "g" "g" "h" "h" "i" "i" "i" "j" "j" "j" "j" "k" "k" "k" "k" "l" "l" "l" "l" "l" "l" "m" "m" "m" "n" "n" "o" "o" "o" "o" "p" "p" "q" "q" "q" "r"
#>  [77] "s" "s" "s" "s" "t" "u" "u" "u" "v" "v" "v" "v" "v" "v" "w" "w" "w" "x" "x" "y" "y" "y" "z" "z"