Skip to contents

A generic function to generate a random permutation of an object. The default method is equivalent to sample(x) without surprises for numeric vectors of length 1. The data.frame method permutes the order of rows.

Usage

permute(x, ...)

# S3 method for data.frame
permute(x, ...)

Arguments

x

A vector or data frame.

...

Additional arguments passed on to methods.

Value

The object x with elements permuted in a random order.

See also

sample() for general purpose random sampling.

Other sampling helpers: resample()

Examples

permute(1:5)
#> [1] 2 3 1 5 4
permute(5) # Compare to sample(5)
#> [1] 5

# The data.frame method permutes rows
permute(iris) |> head()
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 103          7.1         3.0          5.9         2.1  virginica
#> 21           5.4         3.4          1.7         0.2     setosa
#> 95           5.6         2.7          4.2         1.3 versicolor
#> 142          6.9         3.1          5.1         2.3  virginica
#> 112          6.4         2.7          5.3         1.9  virginica
#> 147          6.3         2.5          5.0         1.9  virginica