blk.diff: Blockwise Lagged Differences

View source: R/PCSmisc.R

blk.diffR Documentation

Blockwise Lagged Differences

Description

Computes lagged difference within blocks and on selected elements.

Usage

blk.diff(x, id, ind = NULL, lag = 1, fill = NA, diff.op = "-", ...)
blk.intereventTime(time, id, ind = NULL, lag = 1, fill = NA, diff.op = difftime.default, ...)

Arguments

x,time

A vector in block-format with respect to id.

id

A valid block-format ID.

ind

A logical vector that designates a subset of x. By default all are included.

lag

An integer specifying the lag.

fill

A value to use when no other value is appropriate.

diff.op

A function that subtracts one value from another.

...

Further arguments passed to diff.op.

Details

These functions operate on data sets in block-format. Essentially, the standard diff function is applied within each block, except that a function diff.op can be specified for doing the subtraction. Additionally, a subset on which to perform the operation can be selected with ind. For elements that are not selected the corresponding result is given by fill. The first lag elements of each block are the result are also assigned the value fill so that the result is in block-format with respect to id.

For time values, blk.intereventTime is an alias with a more descriptive name and a different default diff.op.

Value

A vector in block-format with respect to id containing the differenced values.

Author(s)

Benjamin Rich <mail@benjaminrich.net>

See Also

  • block-format

  • diff

  • deltat

Examples

require(nlme)
data(Phenobarb)
dat <- Phenobarb[1:56,]  # First 4 subjects
attach(dat)

cbind(dat, INTERDOSE.TIME=blk.intereventTime(time, asID(Subject), ind=!is.na(dose)))

detach(dat)

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