Skip to contents

Arrange data to distribute small values further apart from each other

Usage

packing(.data, value, level = NULL)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

value

A .data field which contains values to distribute

level

A .data grouping field for distribution

Value

An object of the same type as .data.

Examples

# Create an example
n <- 20
df <- dplyr::tibble(
 lvl1 = sample(LETTERS[1:5], n, TRUE),
 lvl2 = sample(LETTERS[6:24], n, TRUE),
 value = sample(1:20, n, TRUE)
 )

# Arrange all values
packing(df, value)
#> # A tibble: 20 × 3
#>    lvl1  lvl2  value
#>    <chr> <chr> <int>
#>  1 E     I         1
#>  2 E     K        20
#>  3 C     X         3
#>  4 C     V        20
#>  5 E     H         6
#>  6 A     X        18
#>  7 D     M         7
#>  8 E     P        18
#>  9 E     K         8
#> 10 E     X        16
#> 11 E     N         8
#> 12 C     V        16
#> 13 E     Q         9
#> 14 E     L        12
#> 15 C     J         9
#> 16 C     J        12
#> 17 C     R        10
#> 18 A     H        12
#> 19 E     K        11
#> 20 D     L        11

# Arrange values within values
packing(df, value, lvl1)
#> # A tibble: 20 × 3
#>    lvl1  lvl2  value
#>    <chr> <chr> <int>
#>  1 D     M         7
#>  2 D     L        11
#>  3 E     I         1
#>  4 E     K        20
#>  5 E     H         6
#>  6 E     P        18
#>  7 E     K         8
#>  8 E     X        16
#>  9 E     N         8
#> 10 E     L        12
#> 11 E     Q         9
#> 12 E     K        11
#> 13 A     H        12
#> 14 A     X        18
#> 15 C     X         3
#> 16 C     V        20
#> 17 C     J         9
#> 18 C     V        16
#> 19 C     R        10
#> 20 C     J        12