How to randomize a vector

RVectorRandomShuffle

R Problem Overview


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)

R Solutions


Solution 1 - R

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 - R

Use sample function

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

set.seed(001) # just to make it reproducible
sample(V)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1723765View Question on Stackoverflow
Solution 1 - RBen BolkerView Answer on Stackoverflow
Solution 2 - RJilber UrbinaView Answer on Stackoverflow