To shuffle a vector in R, you can use the `sample()` function.
In this example,
int_vector
containing integers.sample()
function to shuffle the elements of the vector.int_vector <- c(1, 2, 3, 4, 5)
shuffled_vector <- sample(int_vector)
print(shuffled_vector)
[1] 3 2 1 5 4
In this example,
str_vector
containing strings.sample()
function to shuffle the elements of the vector.str_vector <- c("apple", "banana", "orange", "grape", "kiwi")
shuffled_vector <- sample(str_vector)
print(shuffled_vector)
[1] "kiwi" "banana" "grape" "apple" "orange"
In this tutorial, we learned How to Shuffle a Vector in R language with well detailed examples.