I have a list of dates that I wish to sample from. Sometimes the sample space will just be a single date e.g. sample("10/11/11",1). The dates are stored as chron objects, so when I have just a single date in my sample space (and only then) sample treats this as a vector (1:date). The documentation for sample points this out:
If ‘x’ has length 1, is numeric (in the sense of ‘is.numeric’) and
‘x >= 1’, sampling _via_ ‘sample’ takes place from ‘1:x’. _Note_
that this convenience feature may lead to undesired behaviour when
‘x’ is of varying length in calls such as ‘sample(x)’. See the
examples.
But I didn't see a way to disable this feature. Is there a workaround or a way to stop it from treating objects of length one as numeric?
The sample
documentation recommends this:
resample <- function(x, ...) x[sample.int(length(x), ...)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With