0

I am trying to sample one element out of a numeric vector.

When the length of the vector > 1, the result is one of the numbers of the vector, as expected. However when the vector contains one element, it samples a number between 0 and this single number.

For example:

sample(c(100, 1000), 1)

results in either 100 or 1000, however

sample(c(100), 1)

results in different numbers smaller than 100.

What is going on?

Omry Atia
  • 2,411
  • 2
  • 14
  • 27

2 Answers2

3

Have a look at the Details of the sample function:

"If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x"

Joachim Schork
  • 2,025
  • 3
  • 25
  • 48
2

This is (unfortunately) expected behavior. See ?sample. The first line of the Details section:

If x has length 1, is numeric (in the sense of is.numeric) and x >= 1, sampling via sample takes place from 1:x. Note that this convenience feature may lead to undesired behaviour when x is of varying length in calls such as sample(x). See the examples.

Luckily the Examples section provides a suggested fix:

# sample()'s surprise -- example
x <- 1:10
    sample(x[x >  8]) # length 2
    sample(x[x >  9]) # oops -- length 10!
    sample(x[x > 10]) # length 0

## safer version:
resample <- function(x, ...) x[sample.int(length(x), ...)]
resample(x[x >  8]) # length 2
resample(x[x >  9]) # length 1
resample(x[x > 10]) # length 0

You could, of course, also just use an if statement:

sampled_x = if (length(my_x) == 1) my_x else sample(my_x, size = 1)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294