scope-package {scope} | R Documentation |
Calculate, per data frame row, a value that depends on information in a relevant subset of rows and columns. These functions create and refine scope objects, which identify relevant rows on a per-row basis. Columns can be aggregated within relevant scopes to aid identification of a row of interest, from which the value in an arbitrary column can be selected.
Package: | scope |
Type: | Package |
Version: | 2.0-2 |
Date: | 2006-10-25 |
License: | GPL version 2 or newer |
Given a data frame of length n, scope()
creates a list of length n, each element of
which is a vector of length n or smaller (a scope object). The vectors hold data frame row names
that are selected by some criterion specified as an argument to scope()
. Scope
objects may be sequentially refined by passing to scope()
with additional
criteria. For a given scope object, score()
returns a vector of the lengths of
each element. scoop()
effectively dereferences the scope object, returning a
vector of values of a specified column for scope elements of length one (1).
skim()
is like tapply()
, applying an aggregate function to cells specified
by scope elements. Some aggregate functions are likely to return values that
occur once among their arguments (often min()
and max()
, but probably not mean()
);
passing such functions to skim()
creates a vector that can be used with scope()
to identify the matching row. probe()
is a short-cut that uses skim()
,
scope()
, and scoop()
to find the value in an arbitrary column where the value in
a related column matches the aggregated value.
Similar results can be achieved with code blocks that merge()
, tapply()
,
aggregate.data.frame()
, etc. The scope()
package is an alternative that
has three advantages: a given manipulation can be expressed in fewer lines of code;
the row and column orders of the source data frame are untouched (cs.
merge()
); and the source-sink mapping is explicit (via the scope
object).
Changes for version 2. scope()
, skim()
, and score
have been vectorized
internally for greater speed. All main functions now accept extra (...) arguments.
The scope
object itself now inherits class "scope", and its elements have
the same names as the row names of the source data frame. A print
function has
been added for scope objects. scope()
may now be invoked with this
having length greater than 1 (if no other arguments are supplied) as a shortcut
for serial "scoping" using the function "==" (similar to tapply()
with
multiple indices).
Tim Bergsma
Maintainer: Tim Bergsma <timb@metrumrg.com>
data(Theoph) #What is the time since the maximum concentration within subject? Scope <- scope(Theoph,'Subject') Tmax <- probe(Theoph,'conc',that='Time',scope=Scope) Theoph$SinceMax <- Theoph$Time - Tmax