96
a<-c(1,2,0,7,5)

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

hhh
  • 50,788
  • 62
  • 179
  • 282
  • 11
    On a side note: You seem like you're new to R and should probably just read some introductory materials. http://stackoverflow.com/questions/420296/what-are-some-good-books-web-resources-and-projects-for-learning-r – Dason Feb 22 '12 at 07:54
  • On another side note: The example data you are providing is a vector, not a list. In case you want to know how to sample an element from a list, you may have a look here: https://statisticsglobe.com/select-random-element-from-list-in-r – Joachim Schork Jan 06 '21 at 13:33

6 Answers6

124
# Sample from the vector 'a' 1 element.
sample(a, 1)
Dason
  • 60,663
  • 9
  • 131
  • 148
  • 14
    Be careful with sample! If `a` has length 1, is numeric (in the sense of is.numeric) and `a` >= 1, sampling via sample takes place from `1:a`. Note that this convenience feature may lead to undesired behavior when `a` is of varying length. – pomber Dec 26 '14 at 03:58
  • Is there any function in R that sample the values and leaves unchosen values as NAs? – hhh Oct 10 '18 at 15:40
  • @hhh Only a few years late but... I don't really understand what you want with that – Dason Apr 05 '23 at 21:35
36

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.

ah bon
  • 9,293
  • 12
  • 65
  • 148
moldovean
  • 3,132
  • 33
  • 36
21

Be careful when using sample!

sample(a, 1) works great for the vector in your example, but when the vector has length 1 it may lead to undesired behavior, it will use the vector 1:a for the sampling.

So if you are trying to pick a random item from a varying length vector, check for the case of length 1!

sampleWithoutSurprises <- function(x) {
  if (length(x) <= 1) {
    return(x)
  } else {
    return(sample(x,1))
  }
}
pomber
  • 23,132
  • 10
  • 81
  • 94
7

This method doesn't produce an error when your vector is length one, and it's simple.

a[sample(1:length(a), 1)]
ah bon
  • 9,293
  • 12
  • 65
  • 148
skan
  • 7,423
  • 14
  • 59
  • 96
5

Read this article about generating random numbers in R.

http://blog.revolutionanalytics.com/2009/02/how-to-choose-a-random-number-in-r.html

You can use sample in this case

sample(a, 1)

Second attribute is showing that you want to get only one random number. To generate number between some range runif function is useful.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
3

An alternative is to select an item from the vector using runif. i.e

a <- c(1,2,0,7,5)
a[runif(1,1,6)]

Lets say you want a function that picks one each time it is run (useful in a simulation for example). So

a <- c(1,2,0,7,5)
sample_fun_a <- function() sample(a, 1)
runif_fun_a <- function() a[runif(1,1,6)]
microbenchmark::microbenchmark(sample_fun_a(), 
                           runif_fun_a(),
                           times = 100000L)

Unit: nanoseconds

sample_fun_a() - 4665

runif_fun_a() - 1400

runif seems to be quicker in this example.

MrHopko
  • 879
  • 1
  • 7
  • 16