[Rd] Function some()

John Fox jfox at mcmaster.ca
Fri Sep 17 19:10:39 CEST 2004


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

-------------- snip ---------------------

# 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]
    }



More information about the R-devel mailing list