position_min {rearrr}R Documentation

Positions the lowest value with values increasing around it

Description

[Experimental]

The lowest value is positioned at the given index/quantile with the other values increasing around it.

Example:

The column values:

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

and position = 2

are ordered as:

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

Usage

position_min(data, col = NULL, position = 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.

position

Index or quantile (in 0-1) at which to position the element of interest.

shuffle_sides

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

Value

The sorted data.frame (tibble) / vector.

Author(s)

Ludvig Renbo Olsen, r-pkgs@ludvigolsen.dk

See Also

Other rearrange functions: center_max(), center_min(), closest_to(), furthest_from(), pair_extremes(), position_max(), 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
)

# Position the smallest index (row number)
position_min(df, position = 3)$index
position_min(df, position = 8)$index

# Position the minimum value in each of the columns
position_min(df, col = "A", position = 3)$A
position_min(df, col = "B", position = 3)$B
position_min(df, col = "C", position = 3)$C

# Randomize which elements are left and right of the position
position_min(df, col = "A", position = 3, shuffle_sides = TRUE)$A

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

# Plot the rearranged values
plot(x = 1:10, y = position_min(df, col = "B", position = 3)$B)
plot(x = 1:10, y = position_min(df, col = "B", position = 3, shuffle_sides = TRUE)$B)


[Package rearrr version 0.3.4 Index]