calc_phase_pairs: Calculate phase pair numbers

View source: R/data-processing.R

calc_phase_pairsR Documentation

Calculate phase pair numbers

Description

Calculates a vector containing a unique phase number for every sequential occurrence of a phase or treatment condition. This is useful for creating a grouping variable to be used in calculating effect sizes for each pair of A-B phases within treatment reversal designs.

Usage

calc_phase_pairs(x, session = seq_along(x))

Arguments

x

vector of phase/condition labels.

session

numeric vector of measurement occasions, used to sort x before calculating phase pairs.

Value

A vector containing an integer phase number for every observation.

Examples

# Basic use-case
x <- rep(c("A","B","C","B","C","A","C"), c(4:10))
calc_phase_pairs(x)

# Using session argument to handle sort order
session <- sample(seq_along(x))
x_scrambled <- x[session]
dat <- data.frame(
  x = x_scrambled,
  session = session,
  phase_pair = calc_phase_pairs(x_scrambled, session = session)
)
dat_sorted <- dat[order(session),]
identical(x, dat_sorted$x)

# With a grouped data.frame
library(dplyr)
Schmidt2007 %>%
  group_by(Behavior_type, Case_pseudonym) %>%
  mutate(phase_pair = calc_phase_pairs(Condition, session = Session_number))


jepusto/SingleCaseES documentation built on Aug. 21, 2023, 12:08 p.m.