[Rd] generic sample
Romain Francois
rfrancois at mango-solutions.com
Thu Nov 9 09:53:51 CET 2006
Hi,
When x is a data frame, sample(x) is not really sensible :
R> sample(iris) # sends back all the iris data set.
What about a generic sample function (from R-devel/src/library/base/R)
with a method for class `data.frame` that would sample the row indexes
and then do the subset. See the code below.
Cheers,
Romain
sample <- function(x, ...)
UseMethod("sample")
sample.data.frame <- function(x, ...){
x[ sample(1:nrow(x), ...), ]
}
sample.default <- function(x, size, replace=FALSE, prob=NULL)
{
if(length(x) == 1 && x >= 1) {
if(missing(size)) size <- x
.Internal(sample(x, size, replace, prob))
}
else {
if(missing(size)) size <- length(x)
x[.Internal(sample(length(x), size, replace, prob))]
}
}
R> set.seed(4)
R> sample(iris, 5)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
88 6.3 2.3 4.4 1.3 versicolor
2 4.9 3.0 1.4 0.2 setosa
44 5.0 3.5 1.6 0.6 setosa
41 5.0 3.5 1.3 0.3 setosa
119 7.7 2.6 6.9 2.3 virginica
--
*mangosolutions*
/data analysis that delivers/
Tel +44 1249 467 467
Fax +44 1249 467 468
More information about the R-devel
mailing list