rshuffle: (Randomized) even shuffling of categories

View source: R/data-wrangling-misc.R

rshuffleR Documentation

(Randomized) even shuffling of categories

Description

(Randomized) even shuffling of categories

Usage

rshuffle(x, mode = c("squish", "symmetric", "repel"), shuffle_within = FALSE)

Arguments

x

Vector

mode

Algorithm for even shuffling:

  • "squish" (default): Within each category, maximize the distance between observations, away from the ends of the vector.

  • "symmetric": Maximize distance around the category with the smallest number of observations.

  • "repel": Within each category, maximize the distance between observations, period.

shuffle_within

Whether observations within each category should be shuffled. Defaults to FALSE, which keeps the ordering of observations within each category.

Value

A vector of shuffled indices of x

Examples


ex <- c(rep("a", 10), rep("bb", 5), rep("ccc", 3), rep("a", 2), rep("bb", 3), rep("ccc", 3))
ex

# Demos of algorithms
barplot(nchar(ex[rshuffle(ex)])) # mode = "squish"
barplot(nchar(ex[rshuffle(ex, mode = "symmetric")]))
barplot(nchar(ex[rshuffle(ex, mode = "repel")]))

# Returns indices for ease of reordering rows
data.frame(id = LETTERS, category = ex)[rshuffle(ex), , drop = FALSE]


yjunechoe/penngradlings documentation built on Sept. 6, 2024, 8:13 p.m.