Search code examples
rsample

r: How to sample from a population of size 1?


I appreciate that sampling from a list of length 1 has little practical use yet all the same I tried the following:

When I run the r snippet sample(c(1,2,3),1) then I obtain a random single value from the list c(1,2,3).

When I run the r snippet sample(c(3),1) then I would expect the number 3 to always be output but I don't, I seem to obtain the same behaviour as above.

Why is this? How can I sample from a list of length 1?

I found that sample(c(3,3),1) does indeed output the intended, but feels not what I had in mind.


Solution

  • See documentation for sample:

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