From murdoch at stats.uwo.ca  Thu Oct  7 12:45:43 2004
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Thu Oct  7 12:45:34 2004
Subject: [Rd] sample suggestion
In-Reply-To: <20041007070001.GA2946@stat.umu.se>
References: <20041007070001.GA2946@stat.umu.se>
Message-ID: <d17am010dk3tafv6gtsjbgpu0ste3tne5k@4ax.com>

On Thu, 7 Oct 2004 09:00:01 +0200, gb@stat.umu.se (G?ran Brostr?m)
wrote:

>I have been bitten by what is clearly described on the help page for
>'sample', namely sampling from a population of size one. I agree that it is
>convenient to have an exception if 'length(x) == 1', but my suggestion is
>to enforce the exception only if 'x' is numeric. In any case, if x is not
>numeric and of length 1, all you get is an error message.
>
>I would like 'sample("tre", 1)' to return "tre", and 'sample(3, 1)' to
>return a natural number less than 4. It seems easy to achieve that by
>changing the line
>
>if (length(x) == 1 && x >= 1) {
>
>to
>
>if (length(x) == 1 && is.numeric(x) && x >= 1) {
>
>in 'sample'.
>
>Isn't that a good idea? An alternative suggestion would be to implement
>'resample' (from the examples) in 'base'.

I agree that's a good suggestion; I think it would be even better to
add an extra argument to sample to say how to handle a length 1
population vector, e.g.

sample(x, size, replace = FALSE, prob = NULL, expand = is.numeric(x))

 ...

if (length(x) == 1 && expand && x >= 1) {

so that the resample() example would be simply

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


The case of zero-length x would still need to be handled separately,
or generate an error.

Duncan Murdoch


[Rd] sample suggestion
Duncan Murdoch