如何在R中生成整数和定义长度的随机向量?

问题描述 投票:0回答:1

我听说过

    set.seed(12)
    runif(12) 

但是我需要在我的向量中使用整数随机数,例如,向量长度为​​5。

喜欢:x<-c(rand,5)(我知道这是假命令,只是为了想法)来获得c = 9,0,4,3,5

而且我想知道如何获得这样的矢量只有15到34之间的数字范围。

谢谢!

r vector random
1个回答
5
投票

我想你想要这样的东西

R> set.seed(123)                     ## ensure it is reproducible
R> sample(15:35, 5, replace=FALSE)   ## you probably want unique draws
[1] 21 30 22 34 32
R> 

对于踢,您也可以对它进行排序:

R> set.seed(123)
R> sort(sample(15:35, 5, replace=FALSE))
[1] 21 22 30 32 34
R> 
© www.soinside.com 2019 - 2024. All rights reserved.