How to randomize a vector

user1723765 picture user1723765 · Dec 7, 2012 · Viewed 84.4k times · Source

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)

Answer

Ben Bolker picture Ben Bolker · Dec 7, 2012

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’).