occasion: Enumerate on condition 1 when followed by condition 2

View source: R/transforms_time.R

occasionR Documentation

Enumerate on condition 1 when followed by condition 2

Description

Enumerate on condition 1 when followed by condition 2

Usage

occasion(x1, x2 = TRUE, .lead_in = c("combine", "separate", "missing"))

Arguments

x1

The main condition on which to enumerate

x2

A predicate condition which must be TRUE following x1 as TRUE, in order to increment the occasion.

.lead_in

One of "combine" (default), "separate" or "missing" to indicate what to do for elements appearing before the first occasion.

Details

x1 and x2 are expressions that evaluate to TRUE or FALSE. When x1 is TRUE a counter is incremented, but only if x2 is TRUE prior to the next value of x1 being TRUE. That is, consecutive x1 TRUE values count as a single occasion. The occasion is only incremented if an x2 value of TRUE falls between TRUE values of x1. Note that x2 can fall anywhere between TRUE values of x1 - the exact position is not specified.

Value

An integer array, of the length of x1, enumerating occasions.

Examples

library(dplyr)
# increment occasion only when 1 is followed by 2.
vals = c(1,2,3,2,3,2,1,1,1,2,3,2,1,2,1,1,2,2,2,1,2,1,2,1,2)
occasion(vals==1, vals==2)
#' # increment occasion only when 1 is followed by 2, .lead_in options.
vals = c(2,1,1,2,3,2,3,2,1,1,1,2,3,2,1,2,1,1,2,2,2,1,2,1,2,1,2)
occasion(vals==1, vals==2, .lead_in="combine")
occasion(vals==1, vals==2, .lead_in="separate")
occasion(vals==1, vals==2, .lead_in="missing")
# increment occasion only when 1 is IMMEDIATELY followed by 2.
vals = c(1,2,3,2,3,2,1,1,1,2,3,2,1,2,1,1,2,2,2,1,2,1,2,1,2)
occasion(vals==1 & lead(vals)==2)
# increment when evid=0 and cmt 2 is observed
evid = c(1,0,0,0,0,0,1,0,0,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,0)
occasion(evid==1, evid==0 & vals==2)

qPharmetra/PMDatR documentation built on April 7, 2024, 5:42 p.m.