| blk.applyNto1 | R Documentation |
Apply a function to each block in a block-format data set
and return the result in a suitable format.
blk.applyNto1(x, id, apply.fun, ind = NULL, fill = NA, ...)
blk.applyNtoN(x, id, apply.fun, ind = NULL, fill = NA, ...)
x |
A vector in |
id |
A valid |
apply.fun |
A function to apply to each block of |
ind |
A vector of logicals used to filter the values of |
fill |
A value to use when no other value is appropriate. See details. |
... |
Further arguments passed to |
These functions operate on data sets in block-format.
The function apply.fun on each block of x, similar to
tapply. The values of x passed to the function
can be filtered by using ind to specify a subset.
If the objective is to extract specific values from x rather
than to use a function to transform the values, then
blk.singleValue or blk.repeatValue
should be used instead.
For blk.applyNto1, the function apply.fun should take
a vector as its first argument and return a vector of length 1. If
the returned vector has length greater than 1, then only the first
element will be kept and a warning issued. If the returned vector
has length 0, then the result for the correponding block is
NA, and a warning is issued.
For blk.applyNtoN, the function apply.fun should take
a vector as its first argument and return a vector. The length of
the returned vector will be extended or reduced as necessary to
obtain a vector matching the length of the function input with
elements recycled if necessary. If the length of the input vector
is not a multiple of the length of the returned vector then a
warning is issued. In general, only functions for which the length
of the returned vector equals to the length of the input or 1
make sense.
The result contains the value fill for blocks of size zero
(see block-format) or for which all ind equals
FALSE.
blk.applyNto1 returns a vector containing one value for
each level of id.
blk.applyNtoN returns a vector in
block-format with respect to id.
Benjamin Rich <mail@benjaminrich.net>
block-format
blk.singleValue, blk.repeatValue
tapply, by, aggregate
require(nlme)
data(Phenobarb)
dat <- Phenobarb[1:56,] # First 4 subjects
attach(dat)
# Find the maximum concentration observed for each subject
cbind(dat, Cmax=blk.applyNtoN(conc, asID(Subject), max, na.rm=TRUE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.