How to randomize a vector

天大地大妈咪最大 提交于 2019-12-17 16:26:40

问题


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)

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




回答2:


Use sample function

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

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


来源:https://stackoverflow.com/questions/13765972/how-to-randomize-a-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!