A generic function to draw elements from an object with replacement. The
default method is equivalent to sample(x, replace = TRUE)
without surprises
for numeric vectors of length 1. The data.frame
method resamples rows.
Arguments
- x
A vector or data frame.
- size
A positive integer. The number of elements to sample.
- prob
A numeric vector of sampling probability weights for each element in
x
.- ...
Additional arguments passed on to methods.
Examples
resample(1:5)
#> [1] 5 5 1 5 5
resample(5) # Compare to sample(5, replace = TRUE)
#> [1] 5
# The data.frame method samples rows
head(iris) |> resample()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 3 4.7 3.2 1.3 0.2 setosa
#> 3.1 4.7 3.2 1.3 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 1 5.1 3.5 1.4 0.2 setosa
#> 1.1 5.1 3.5 1.4 0.2 setosa