center_min {rearrr}R Documentation

Centers the lowest value with values increasing around it

Description

[Experimental]

The lowest value is positioned in the middle with the other values increasing around it.

Example:

The column values:

c(1, 2, 3, 4, 5)

are ordered as:

c(5, 3, 1, 2, 4)

Usage

center_min(data, col = NULL, shuffle_sides = FALSE)

Arguments

data

data.frame or vector.

col

Column to create sorting factor by. When `NULL` and `data` is a data.frame, the row numbers are used.

shuffle_sides

Whether to shuffle which elements are left and right of the center. (Logical)

Value

The sorted data.frame (tibble) / vector.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other rearrange functions: center_max(), closest_to(), furthest_from(), pair_extremes(), position_max(), position_min(), rev_windows(), roll_elements(), shuffle_hierarchy(), triplet_extremes()

Examples

# Attach packages
library(rearrr)
library(dplyr)

# Set seed
set.seed(1)

# Create a data frame
df <- data.frame(
  "index" = 1:10,
  "A" = sample(1:10),
  "B" = runif(10),
  "C" = LETTERS[1:10],
  "G" = c(
    1, 1, 1, 2, 2,
    2, 3, 3, 3, 3
  ),
  stringsAsFactors = FALSE
)

# Center by the index (row numbers)
center_min(df)

# Center by each of the columns
center_min(df, col = "A")
center_min(df, col = "B")
center_min(df, col = "C")

# Randomize which elements are left and right of the center
center_min(df, col = "A", shuffle_sides = TRUE)

# Grouped by G
df %>%
  dplyr::select(G, A) %>% # For clarity
  dplyr::group_by(G) %>%
  center_min(col = "A")

# Plot the centered values
plot(x = 1:10, y = center_min(df, col = "B")$B)
plot(x = 1:10, y = center_min(df, col = "B", shuffle_sides = TRUE)$B)

[Package rearrr version 0.3.4 Index]