View source: R/S03_Utilities.R
match_and_reorder | R Documentation |
Function to match a vector of values against another vector and reorder the original vector based on the matching indices.
match_and_reorder(x, values, y = NULL)
x |
A vector. |
values |
A vector containing the values to match
against in |
y |
An optional vector matching in length to |
Either a vector of indices for reordering or the
reordered output from the input y
.
x <- rep( LETTERS[1:3], 3 )
values <- c( 'C', 'B', 'A' )
y <- rep( 1:3, 3 )
match_and_reorder( x, values, y )
set.seed( 111 ) # For reproducibility
# Example data frame
dtf_example <- data.frame(
X = rep( 1:4, each = 2 ),
Y = round( rnorm( 8 ), 2 )
)
# Resample with replacement from 'X'
shuffled_x <- sample( 1:4, size = 4, replace = TRUE )
# Create a reordered data frame based on the resampled values
dtf_shuffled <- data.frame(
X = match_and_reorder( dtf_example$X, shuffled_x, dtf_example$X ),
Y = match_and_reorder( dtf_example$X, shuffled_x, dtf_example$Y )
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.