Take a Sample from a Data Frame-like Object

Description

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 signature 'data.frame'
sample(x, size, replace = FALSE, prob = NULL)
## S4 method for signature 'data.set'
sample(x, size, replace = FALSE, prob = NULL)
## S4 method for signature '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
144          6.8         3.2          5.9         2.3  virginica
82           5.5         2.4          3.7         1.0 versicolor
77           6.8         2.8          4.8         1.4 versicolor
54           5.5         2.3          4.0         1.3 versicolor
3            4.7         3.2          1.3         0.2     setosa
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
94           5.0         2.3          3.3         1.0 versicolor
25           4.8         3.4          1.9         0.2     setosa
117          6.5         3.0          5.5         1.8  virginica
30           4.7         3.2          1.6         0.2     setosa
79           6.0         2.9          4.5         1.5 versicolor
   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
63          6.0         2.2          4.0         1.0 versicolor
64          6.1         2.9          4.7         1.4 versicolor
84          6.0         2.7          5.1         1.6 versicolor
71          5.9         3.2          4.8         1.8 versicolor
88          6.3         2.3          4.4         1.3 versicolor
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
81           5.5         2.4          3.8         1.1 versicolor
72           6.1         2.8          4.0         1.3 versicolor
94           5.0         2.3          3.3         1.0 versicolor
125          6.7         3.3          5.7         2.1  virginica
98           6.2         2.9          4.3         1.3 versicolor