Last active
March 31, 2025 14:04
-
-
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 ...]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ripple_shuffle <- function(object) { | |
purrr::map( | |
seq(1, length(object) / 2), | |
~ c( | |
purrr::chuck(object, .x), | |
purrr::chuck(object, length(object) - .x + 1) | |
) | |
) %>% | |
unlist() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
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
This results in sometimes dropping an element!