From jfox at mcmaster.ca  Fri Sep 17 19:10:39 2004
From: jfox at mcmaster.ca (John Fox)
Date: Fri Sep 17 19:10:42 2004
Subject: [Rd] Function some()
Message-ID: <20040917171038.YRUK7925.tomts25-srv.bellnexxia.net@JohnDesktop8300>

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


[Rd] Function some()
John Fox