# Attach packages
library(rearrr)
library(dplyr)
# Set seed
set.seed(1)
# Create a data frame
df <- data.frame(
"index" = 1:12,
"A" = sample(1:12),
"B" = runif(12),
"C" = LETTERS[1:12],
"G" = c(
1, 1, 1, 1, 2, 2,
2, 2, 3, 3, 3, 3
),
stringsAsFactors = FALSE
)
# Triplet group extreme indices (row numbers)
triplet_extremes(df)
# Triplet group extremes in each of the columns
triplet_extremes(df, col = "A")$A
triplet_extremes(df, col = "B")$B
triplet_extremes(df, col = "C")$C
# Shuffle the members triplet-wise
# The triplets maintain their order
# but the rows within each triplet are shuffled
triplet_extremes(df, col = "A", shuffle_members = TRUE)
# Shuffle the order of the triplets
# The triplets are shuffled but
# the rows within each triplet maintain their order
triplet_extremes(df, col = "A", shuffle_triplets = TRUE)
# Use recursive grouping
# Mostly meaningful with much larger datasets
# Order initial grouping by group identifiers
triplet_extremes(df, col = "A", num_groupings = 2)
# Order initial grouping by aggregate values
triplet_extremes(df, col = "A", num_groupings = 2, order_by_aggregates = TRUE)
# Grouped by G
# Each G group only has 4 elements
# so it only creates 1 triplet and a group
# with the single excessive element
# per G group
df %>%
dplyr::select(G, A) %>% # For clarity
dplyr::group_by(G) %>%
triplet_extremes(col = "A")
# Plot the extreme triplets
plot(
x = 1:12,
y = triplet_extremes(df, col = "A")$A,
col = as.character(rep(1:4, each = 3))
)
# With shuffled triplet members (run a few times)
plot(
x = 1:12,
y = triplet_extremes(df, col = "A", shuffle_members = TRUE)$A,
col = as.character(rep(1:4, each = 3))
)
# With shuffled triplets (run a few times)
plot(
x = rep(1:6, each = 2),
y = triplet_extremes(df, col = "A", shuffle_triplets = TRUE)$A,
col = as.character(rep(1:4, each = 3))
)
Run the code above in your browser using DataLab