position_max {rearrr}R Documentation

Positions the highest values with values decreasing around it

Description

[Experimental]

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

Example:

The column values:

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

and position = 2

are ordered as:

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

Usage

position_max(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_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
)

# Position the highest index (row number)
position_max(df, position = 3)$index
position_max(df, position = 8)$index

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

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

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

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

[Package rearrr version 0.3.4 Index]