[Rd] generic sample

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Thu 09 Nov 2006 - 08:53:51 GMT


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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri Nov 10 06:08:36 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 09 Nov 2006 - 20:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.