a<-c(1,2,0,7,5)
Some languages have a picker -function -- choose one random number from a
-- how in R?
There could be many ways to randomly select elements from an R vector. You can use the sample() or the runif() function to select elements from the vector.
choice, sample, choices) In Python, you can randomly sample elements from a list with choice() , sample() , and choices() of the random module. These functions can also be applied to a string and tuple.
# Sample from the vector 'a' 1 element. sample(a, 1)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With