Skip to contents

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.

Usage

resample(x, size = length(x), prob = NULL, ...)

# S3 method for data.frame
resample(x, size = nrow(x), prob = NULL, ...)

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.

Value

An object with size elements drawn from x with replacement.

See also

sample() for general purpose random sampling.

Other sampling helpers: permute()

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