Select random element in a list of R?

后端 未结 6 1221
太阳男子
太阳男子 2020-12-29 19:19
a<-c(1,2,0,7,5)

Some languages have a picker -function -- choose one random number from a -- how in R?

6条回答
  •  独厮守ぢ
    2020-12-29 20:02

    the above answers are technically correct:

    sample(a,1)
    

    however, if you would like to repeat this process many times, let's say you would like to imitate throwing a dice, then you need to add:

    a<-c(1,2,3,4,5,6)
    sample(a, 12, replace=TRUE)
    

    Hope it helps.

提交回复
热议问题