[R] using "sample()" for a vector of length 1

Jon BR jonsleepy at gmail.com
Thu Jul 22 22:31:04 CEST 2010


Hi All,
    I'm trying to use the "sample" function within a loop where the
vector being sampled from (the first argument in the function) will
vary in length and composition.  When the vector is down in size to
containing only one element, I run into the "undesired behaviour"
acknowledged in the ?sample help file.  I don't want sample(10,1) to
return a number from within 1:10, but rather I'd just want it to
return 10 every time.

Example):


Actual:
> sample(10,1)
[1] 2
> sample(10,1)
[1] 9
> sample(10,1)
[1] 4


Desired:
> sample(10,1)
[1] 10
> sample(10,1)
[1] 10
> sample(10,1)
[1] 10


Perhaps sample is not the appropriate function.  I dunno.  Any thoughts?

Regards,
Jonathan



More information about the R-help mailing list