[Rd] Function some()

From: John Fox <jfox_at_mcmaster.ca>
Date: Sat 18 Sep 2004 - 03:10:39 EST


Dear R-devel list members,

Probably not an opportune time for this, given the immanent release of 2.0.0, but I was just reminded (while preparing a lecture) of a function that I find useful and that I though might be a candidate for the utils package: The function, which I call some(), behaves like head() and tail(), from which it is adapted, except that it samples from an object (e.g., rows of a data frame).

I like to use some() to get a quick look at data. Perhaps there's some other already existing function that I'm not aware of that does the same thing.

Regards,
 John

# adapted from head() and tail()

some <- function(x, ...) UseMethod("some")

some.default <- function(x, n=10, ...){

    len <- length(x)
    ans <- x[sort(sample(len, min(n, len)))]     if (length(dim(x)) == 1)

        array(ans, n, list(names(ans)))
    else ans
}

some.matrix <- function(x, n=10, ...){
  nr <- nrow(x)
  x[sort(sample(nr, min(n, nr))), , drop = FALSE]   }

some.data.frame <- function(x, n=10, ...){

    nr <- nrow(x)
    x[sort(sample(nr, min(n, nr))), , drop=FALSE]
}



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat Sep 18 03:14:13 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:00:18 EST