[Rd] error from sample if sampling length one character (PR#2546)

patrick@burns-stat.com patrick@burns-stat.com
Thu Feb 13 21:57:03 2003


Full_Name: Patrick Burns
Version: 1.6.1
OS: Suse Linux
Submission from: (NULL) (217.36.14.68)



Sample gives an error if it is sampling a length one character
vector:

> sample('a', 1)
Error in sample(x, size, replace, prob) : invalid first argument

The fix is trivial (numeric test in first line of the body):

function (x, size, replace = FALSE, prob = NULL)
{
    if (length(x) == 1 && is.numeric(x) && x >= 1) {
        if (missing(size))
            size <- x
        .Internal(sample(x, size, replace, prob))
    }
    else {
        if (missing(size))
            size <- length(x)
        x[.Internal(sample(length(x), size, replace, prob))]
    }
}

> sample.fixed('a', 1)
[1] "a"