Learn R Programming

DataCombine (version 0.2.21)

CountSpell: Count spells, including for grouped data

Description

CountSpell is a function that returns a variable counting the spell number for an observation. Works with grouped data.

Usage

CountSpell(data, TimeVar, SpellVar, GroupVar, NewVar, SpellValue)

Arguments

data
a data frame object.
TimeVar
a character string naming the time variable.
SpellVar
a character string naming the variable with information on when each spell starts.
GroupVar
a character string naming the variable grouping the units experiencing the spells.
NewVar
NewVar a character string naming the new variable to place the spell counts in.
SpellValue
a value indicating when a unit is in a spell. Must match the class of the SpellVar.

Examples

Run this code
# Create fake data
ID <- sort(rep(seq(1:4), 5))
Time <- rep(1:20)
Dummy <-  c(1, sample(c(0, 1), size = 19, replace = TRUE))
Data <- data.frame(ID, Time, Dummy)

# Find spell for whole data frame
DataSpell1 <- CountSpell(Data, TimeVar = 'Time', SpellVar = 'Dummy',
                         SpellValue = 1)

head(DataSpell1)

# Find spell for each ID group
DataSpell2 <- CountSpell(Data, TimeVar = 'Time', SpellVar = 'Dummy',
                         GroupVar = 'ID', SpellValue = 1)

head(DataSpell2)

Run the code above in your browser using DataLab