0

I'm wondering if there is a version of sample() in R, whether built in or in a package, that always treats x as a vector to sample from rather than as an integer to samples numbers from 1 to x. My use case is using this programmatically where I don't want to get the second behaviour if x is of length 1.

Although a similar question was posted in Sample from vector of varying length (including 1), this doesn't answer if there is a solution bundled in a package. I've implemented a fix in my own function, but I am considering whether this is worth putting as a convenience function in a CRAN package? I want to know if someone has already solved this as a package.

sampleFrom <- function(x, size, replace = FALSE, ...) {
  # Sometimes the input is only of length one, causing different behaviour
  if(length(x) == 1) {
    if(missing(size)){
      return(x)
    } else {
      if(replace) {
        return(rep(x, size))
      } else {
        # Deliberately run a command that will cause an error
        # to get the error message while respecting copyright.
        sample(1, 2, replace = FALSE)
      }
    }
  }
  sample(x, size, replace = replace, ...)
}
trickytank
  • 46
  • 4
  • 1
    I think you're in good company with your observations. `?sample` itself addresses the length=1 & is-integer situation further down in the documentation and in the examples (see the safer `resample` function). – Maurits Evers Sep 04 '22 at 04:38
  • See also the related posts on SO: [here](https://stackoverflow.com/questions/47957321/sample-with-only-1-number), [here](https://stackoverflow.com/questions/64822445/r-how-to-sample-from-a-population-of-size-1), and [here](https://stackoverflow.com/questions/13990125/sample-from-vector-of-varying-length-including-1); in particular the last link has an answer with a function that mirrors yours. I'm closing this as a dupe for now. Please edit your post if you feel that this is fundamentally different from the issues & discussions in the links I give. – Maurits Evers Sep 04 '22 at 04:40
  • I don't feel that the other answers are asking the same question. I want to know if this functionality is available in another package, so if not, I can submit a package to CRAN. – trickytank Sep 04 '22 at 04:48
  • I didn't catch the resample in the other post, since I was looking for a pre made function. I guess I think it would be good to not have to code in the extra function for the common use case. – trickytank Sep 04 '22 at 04:51
  • gdata::resample seems to be what I'm looking for. (I didn't see that in the other answers for other questions). In that way I feel this wasn't a duplicate question. – trickytank Sep 04 '22 at 04:56
  • While I agree with you that your post has a (slightly) different angle compared to the other posts, the underlying issue is the same; additionally, questions asking for functionality in packages/tools etc. are considered off-topic. IMO, I don't see how this would warrant the creation of a package and a CRAN submission. This is a well-documented "quirk" of `sample`. If it makes your life easier to define a new function, great! Perhaps integrate this function into a broader R package you're developing. But a stand-alone R package just to address this quirk in `sample`? That's unnecessary IMO. – Maurits Evers Sep 04 '22 at 05:05
  • Ok thanks. As I didn't find what I was looking for in the other answers, I'm going to add the gdata::resample solution to those, as I think others deserve to know that solution. Thanks – trickytank Sep 04 '22 at 05:12
  • Great! I think adding the `gdata::resample` solution to the existing answers is very useful. – Maurits Evers Sep 04 '22 at 05:16

0 Answers0