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"

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"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top