Learn R Programming

TaoTeProgramming (version 1.0)

safesample: Safely Sample a Single Item

Description

Single random selection from the input vector.

Usage

safesample(x)

Arguments

x
a vector.

Value

a random element of the input x.

Side effects

modify the random seed.

Details

This works around the infelicity of the sample function that can give the wrong result if x has length 1. See Circle 8.2.33 of 'The R Inferno'.

References

http://www.burns-stat.com/documents/books/the-r-inferno/

See Also

sample.

Examples

Run this code
safesample(2:6) # like 'sample(2:6, 1)'

safesample(4) # still works as intended

#compare:
sample(4)

Run the code above in your browser using DataLab