[Rd] Add-on argument in sample()
Henrik Bengtsson
hb at biostat.ucsf.edu
Mon Jun 15 20:31:13 CEST 2015
You're not the first one, e.g.
https://stat.ethz.ch/pipermail/r-devel/2010-March/057029.html
https://stat.ethz.ch/pipermail/r-devel/2010-November/058981.html
(I was bitten by this in a resampling scheme where the set sampled
from was data driven).
Here's a simple solution - taken from R.utils::resample();
> resample <- function (x, ...) x[sample.int(length(x), ...)]
> resample(10, size = 1, replace = FALSE)
[1] 10
> resample(10, size = 3, replace = TRUE)
[1] 10 10 10
> resample(10, size = 3, replace = FALSE)
Error in sample.int(length(x), ...) :
cannot take a sample larger than the population when 'replace = FALSE'
/Henrik
On Mon, Jun 15, 2015 at 5:55 AM, Millot Gael <Gael.Millot at curie.fr> wrote:
> Hi.
>
> I have a problem with the default behavior of sample(), which performs sample(1:x) when x is a single value.
> This behavior is well explained in ?sample.
> However, this behavior is annoying when the number of value is not predictable. Would it be possible to add an argument
> that desactivates this and perform the sampling on a single value ? Examples:
>> sample(10, size = 1, replace = FALSE)
> 10
>
>> sample(10, size = 3, replace = TRUE)
> 10 10 10
>
>> sample(10, size = 3, replace = FALSE)
> Error
>
> Many thanks for your help.
>
> Best wishes,
>
> Gael Millot.
>
>
> Gael Millot
> UMR 3244 (IC-CNRS-UPMC) et Universite Pierre et Marie Curie
> Equipe Recombinaison et instabilite genetique
> Pav Trouillet Rossignol 5eme etage
> Institut Curie
> 26 rue d'Ulm
> 75248 Paris Cedex 05
> FRANCE
> tel : 33 1 56 24 66 34
> fax : 33 1 56 24 66 44
> Email : gael.millot at curie.fr
> http://perso.curie.fr/Gael.Millot/index.html
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list