How to Shuffle a Vector in R Programming (Example Code)
On this page you’ll learn how to shuffle a vector or array randomly in the R programming language.
Creation of Example Data
my_vec <- LETTERS[1:5] # Creating example vector my_vec # Printing vector to RStudio console # "A" "B" "C" "D" "E" |
my_vec <- LETTERS[1:5] # Creating example vector my_vec # Printing vector to RStudio console # "A" "B" "C" "D" "E"
Example: Shuffle Vector Randomly in R
set.seed(7446834) # Setting the seed my_random_vec <- sample(my_vec) # Sampling the vector my_random_vec # Printing mixed vector # "D" "B" "A" "E" "C" |
set.seed(7446834) # Setting the seed my_random_vec <- sample(my_vec) # Sampling the vector my_random_vec # Printing mixed vector # "D" "B" "A" "E" "C"