The function base::sample() accepts either a vector of valuesto sample from, or a single numeric value that is turned into a vector from 1 to the input value and this range is sampled from. This has resulted in a rather frustrating (and perplexing) error for me in the past. This function, vsample() always assumes the input is a vector. Therefore, if the input vector is of length 1, that value is just returned (i.e. sampling 1 value from a vector of length 1 will always select the single element).

vsample(x, size = length(x), replace = FALSE, prob = NULL)

Arguments

x

either a vector of one or more elements from which to choose, or a positive integer. See ‘Details.’

size

a non-negative integer giving the number of items to choose.

replace

should sampling be with replacement?

prob

a vector of probability weights for obtaining the elements of the vector being sampled.

Value

For sample a vector of length size with elements drawn from either x

Examples

sample(10)
#> [1] 5 1 4 7 10 3 6 8 9 2
vsample(10)
#> [1] 10
vsample(10, size = 3, replace = TRUE)
#> [1] 10 10 10
sample(c(1, 2, 3))
#> [1] 3 1 2
vsample(c(1, 2, 3))
#> [1] 1 2 3