Skip to content

Instantly share code, notes, and snippets.

@PietrH
Last active March 31, 2025 14:04
Show Gist options
  • Save PietrH/83e7bcd62ded101039a33a64ce18b0e1 to your computer and use it in GitHub Desktop.
Save PietrH/83e7bcd62ded101039a33a64ce18b0e1 to your computer and use it in GitHub Desktop.
Ripple shuffle a vector in R so the order is: [first, last, second, second to last ...]
ripple_shuffle <- function(object) {
purrr::map(
seq(1, length(object) / 2),
~ c(
purrr::chuck(object, .x),
purrr::chuck(object, length(object) - .x + 1)
)
) %>%
unlist()
}
ripple_shuffle <- function(object) {
purrr::map(
seq(object),
~ c(
purrr::chuck(object, .x),
purrr::chuck(object, length(object) - .x + 1)
)
) %>%
purrr::map(sort) %>%
unique() %>%
unlist() %>%
unique()
}
@PietrH
Copy link
Author

PietrH commented Mar 31, 2025

This results in sometimes dropping an element!

@PietrH
Copy link
Author

PietrH commented Mar 31, 2025

Added a much less elegant version that does guarantee all elements are at least present

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment