How to randomize a vector

100,225

Solution 1

Yes.

sample(V)

From ?sample:

For ‘sample’ the default for ‘size’ is the number of items inferred from the first argument, so that ‘sample(x)’ generates a random permutation of the elements of ‘x’ (or ‘1:x’).

Solution 2

Use sample function

V<-rep(1:10, each=150)

set.seed(001) # just to make it reproducible
sample(V)
Share:
100,225
user1723765
Author by

user1723765

Updated on July 08, 2022

Comments

  • user1723765
    user1723765 almost 2 years

    I would like to randomly reorganize the order of the numbers in a vector, in a simple one-line command?

    My particular vector V has 150 entries for each value from 1 to 10:

    V <- rep(1:10, each=150)