sample()
that always assumes input is a vector of optionsR/vsample.R
vsample.Rd
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)
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. |
For sample a vector of length size with elements drawn from either x
sample(10)#> [1] 5 1 4 7 10 3 6 8 9 2vsample(10)#> [1] 10vsample(10, size = 3, replace = TRUE)#> [1] 10 10 10#> [1] 3 1 2#> [1] 1 2 3