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 class 'data.frame'
sample(x, size, replace = FALSE, prob = NULL)
# S4 method for class 'data.set'
sample(x, size, replace = FALSE, prob = NULL)
# S4 method for class '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
#> 114          5.7         2.5          5.0         2.0  virginica
#> 133          6.4         2.8          5.6         2.2  virginica
#> 79           6.0         2.9          4.5         1.5 versicolor
#> 140          6.9         3.1          5.4         2.1  virginica
#> 83           5.8         2.7          3.9         1.2 versicolor
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 113          6.8         3.0          5.5         2.1  virginica
#> 119          7.7         2.6          6.9         2.3  virginica
#> 50           5.0         3.3          1.4         0.2     setosa
#> 53           6.9         3.1          4.9         1.5 versicolor
#> 91           5.5         2.6          4.4         1.2 versicolor
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 116          6.4         3.2          5.3         2.3  virginica
#> 56           5.7         2.8          4.5         1.3 versicolor
#> 143          5.8         2.7          5.1         1.9  virginica
#> 3            4.7         3.2          1.3         0.2     setosa
#> 119          7.7         2.6          6.9         2.3  virginica
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 22           5.1         3.7          1.5         0.4     setosa
#> 92           6.1         3.0          4.6         1.4 versicolor
#> 120          6.0         2.2          5.0         1.5  virginica
#> 88           6.3         2.3          4.4         1.3 versicolor
#> 68           5.8         2.7          4.1         1.0 versicolor