# Attach packages
library(rearrr)
library(dplyr)
df <- data.frame(
'a' = rep(1:4, each = 4),
'b' = rep(1:8, each = 2),
'c' = 1:16
)
# Set seed for reproducibility
set.seed(2)
# Shuffle all columns
shuffle_hierarchy(df, group_cols = c('a', 'b', 'c'))
# Don't shuffle 'b' but keep grouping by it
# So 'c' will be shuffled within each group in 'b'
shuffle_hierarchy(
data = df,
group_cols = c('a', 'b', 'c'),
cols_to_shuffle = c('a', 'c')
)
# Shuffle 'b' as if it's not a group column
# so elements are independent within their group
# (i.e. same-valued elements are not necessarily ordered sequentially)
shuffle_hierarchy(df, group_cols = c('a', 'b'), leaf_has_groups = FALSE)
Run the code above in your browser using DataLab