blk.applyNto1: Apply a Function to Each Block.

View source: R/PCSmisc.R

blk.applyNto1R Documentation

Apply a Function to Each Block.

Description

Apply a function to each block in a block-format data set and return the result in a suitable format.

Usage

blk.applyNto1(x, id, apply.fun, ind = NULL, fill = NA, ...)
blk.applyNtoN(x, id, apply.fun, ind = NULL, fill = NA, ...)

Arguments

x

A vector in block-format with respect to id.

id

A valid block-format ID for the input x.

apply.fun

A function to apply to each block of x. See details.

ind

A vector of logicals used to filter the values of x.

fill

A value to use when no other value is appropriate. See details.

...

Further arguments passed to apply.fun.

Details

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.

Value

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.

Author(s)

Benjamin Rich <mail@benjaminrich.net>

See Also

  • block-format

  • blk.singleValue, blk.repeatValue

  • tapply, by, aggregate

Examples


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))


benjaminrich/PCSmisc documentation built on Feb. 11, 2024, 9:25 p.m.