[Rd] proposed change to 'sample'

From: Patrick Burns <pburns_at_pburns.seanet.com>
Date: Sun, 20 Jun 2010 11:07:53 +0100

There is a weakness in the 'sample'
function that is highlighted in the
help file. The 'x' argument can be
either the vector from which to sample,
or the maximum value of the sequence
from which to sample.

This can be ambiguous if the length of
'x' is one.

I propose adding an argument that allows the user (programmer) to avoid that

function (x, size, replace = FALSE, prob = NULL,

     max = length(x) == 1L && is.numeric(x) && x >= 1) {

     if (max) {
         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))]

<environment: namespace:base>

This just takes the condition of the first
'if' to be the default value of the new 'max'

So in the "surprise" section of the examples in the 'sample' help file

sample(x[x > 9])


sample(x[x > 9], max=FALSE)

have different behaviours.

By the way, I'm certainly not convinced that
'max' is the best name for the argument.

Patrick Burns
(home of 'Some hints for the R beginner'
and 'The R Inferno')

R-devel_at_r-project.org mailing list
Received on Sun 20 Jun 2010 - 10:10:19 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 20 Jun 2010 - 21:21:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive