blk.shift: Shift Values Respecting Block Boundaries

View source: R/PCSmisc.R

blk.shiftR Documentation

Shift Values Respecting Block Boundaries

Description

Shifts values up or down by a given number of positions within blocks and on selected elements.

Usage

blk.shift(x, id, shift.by = 1, ind = NULL, fill = NA)

Arguments

x

A vector in block-format with respect to id.

id

A valid block-format ID.

shift.by

An integer specifying the number of positions to shift by. Can be positive (shift down) or negative (shift up).

ind

A logical vector that designates a subset of x to use. By default all are selected.

fill

A value to use when no other value is appropriate.

Details

This function operates on data sets in block-format. The number of positions by which to shift by may be positive or negative, with positive indicating downwards in the data set and negative upwards. 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. Shifting creates ‘gaps’ at the start (or end if shift.by is negative) of a block. The gaps are filled with the value fill so that the result is in block-format with respect to id.

Value

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

Author(s)

Benjamin Rich <mail@benjaminrich.net>

See Also

  • block-format

  • lag

Examples

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

cbind(dat, SHIFT=blk.shift(time, asID(Subject), 2))

detach(dat)

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