rev_windows {rearrr}R Documentation

Reverse order window-wise

Description

[Experimental]

The values are windowed and reversed within windows.

The *_vec() version takes and returns a vector.

Example:

The column values:

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

With window_size = 3

Are ordered as:

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

Usage

rev_windows(data, window_size, factor_name = ".window", overwrite = FALSE)

rev_windows_vec(data, window_size)

Arguments

data

data.frame or vector.

window_size

Size of the windows. (Logical)

factor_name

Name of the factor with window identifiers. If `NULL`, no column is added.

overwrite

Whether to allow overwriting of existing columns. (Logical)

Value

The sorted data.frame (tibble) / vector. Optionally with the windows factor added.

When `data` is a vector and `keep_windows` is FALSE, the output will be a vector. Otherwise, a data.frame.

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(), position_min(), 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" = rep(1:2, each = 5),
  stringsAsFactors = FALSE
)

# For vector
rev_windows_vec(1:10, window_size = 3)

# For data frame
rev_windows(df, window_size = 3)
rev_windows(df, window_size = 3, factor_name = NULL)

# Grouped by G
df %>%
  dplyr::select(G, index) %>% # For clarity
  dplyr::group_by(G) %>%
  rev_windows(window_size = 3)

# Plot the extreme pairs
plot(
  x = 1:10,
  y = rev_windows_vec(1:10, window_size = 3)
)

[Package rearrr version 0.3.4 Index]