Skip to contents

The methods below are convenience short-cuts to take samples from data frames and data sets. They result in a data frame or data set, respectively, the rows of which are a sample of the complete data frame/data set.

Usage

# S4 method for data.frame
sample(x, size, replace = FALSE, prob = NULL)
# S4 method for data.set
sample(x, size, replace = FALSE, prob = NULL)
# S4 method for importer
sample(x, size, replace = FALSE, prob = NULL)

Arguments

x

a data frame or data set.

size

an (optional) numerical value, the sample size, defaults to the total number of rows of x.

replace

a logical value, determines whether sampling takes place with or without replacement.

prob

a vector of sampling probabities or NULL.

Value

A data frame or data set.

Examples

for(.i in 1:4)
  print(sample(iris,5))
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 74           6.1         2.8          4.7         1.2 versicolor
#> 136          7.7         3.0          6.1         2.3  virginica
#> 129          6.4         2.8          5.6         2.1  virginica
#> 38           4.9         3.6          1.4         0.1     setosa
#> 81           5.5         2.4          3.8         1.1 versicolor
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 10          4.9         3.1          1.5         0.1     setosa
#> 55          6.5         2.8          4.6         1.5 versicolor
#> 98          6.2         2.9          4.3         1.3 versicolor
#> 40          5.1         3.4          1.5         0.2     setosa
#> 15          5.8         4.0          1.2         0.2     setosa
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 60           5.2         2.7          3.9         1.4 versicolor
#> 12           4.8         3.4          1.6         0.2     setosa
#> 50           5.0         3.3          1.4         0.2     setosa
#> 103          7.1         3.0          5.9         2.1  virginica
#> 2            4.9         3.0          1.4         0.2     setosa
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 148          6.5         3.0          5.2         2.0  virginica
#> 68           5.8         2.7          4.1         1.0 versicolor
#> 145          6.7         3.3          5.7         2.5  virginica
#> 95           5.6         2.7          4.2         1.3 versicolor
#> 18           5.1         3.5          1.4         0.3     setosa