[Rd] Add-on argument in sample()

Radford Neal radford at cs.toronto.edu
Wed Jun 17 16:18:03 CEST 2015


> Then the question would be if this test could be replaced with a new 
> argument to sample, e.g. expandSingle, which has TRUE as default for 
> backward compatibility, but FALSE if you dont want population to be 
> expanded to 1:population. It could certainly be useful in some cases, 
> but you still need to know about the expansion to use it. I think most 
> of these bugs occur because users did not think about the expansion in 
> the first place or did not realize that their population could be of 
> length 1 in some situations. These users would therefore not think about 
> changing the argument either.

I think the solution might be to make sample always treat the first
argument as the vector to sample from if it has a "dim" attribute that
explicitly specifies that it is a one-dimensional array.  The effect
of this would be that sample(10,1) would sample from 1:10, as at
present, but sample(array(10),1) would sample from the length-one
vector with element 10 (and hence always return 10).

With this change, you can easily ensure that sample(v,1) always samples
from v even when it has length one by rewriting it to sample(array(v),1).

It's of course possible that some existing code relies on the current
behaviour, but probably not much existing code, since one-dimensional
arrays are (I think) not very common at present.

A bigger gain would come if one also introduced a new sequence operator
that creates a sequence that is marked as a one-dimensional array, which
would be part of a solution to several other problems as well, as I
propose at http://www.cs.utoronto.ca/~radford/ftp/R-lang-ext.pdf

   Radford Neal



More information about the R-devel mailing list