convertToWideFormat: Convert simulation output to wide format

View source: R/convert_wide.R

convertToWideFormatR Documentation

Convert simulation output to wide format

Description

Reshapes the population table returned by micSim() into one row per individual, with numbered columns for their recorded transitions.

Usage

convertToWideFormat(pop)

Arguments

pop

The population data frame returned by micSim().

Details

Each transition is represented by four columns: its origin state, destination state, date and age. The first transition appears in From.1, To.1, transitionTime.1 and transitionAge.1; the second appears in the corresponding .2 columns, and so on.

Transitions are numbered in their existing order within each individual, so the input rows should already be in chronological order. Individuals with fewer transitions have NA in the remaining numbered columns.

ns counts an individual's transitions. Someone who never transitioned occupies one row of pop, with NA in all four transition columns, and is reported here with ns = 0.

The output omits motherID. If parent links are needed, retain a separate table of ID and motherID from pop, then join it to the wide result by ID.

The function takes a single data frame, so it is pipe-friendly: pop |> convertToWideFormat().

Value

A data frame with one row per individual and columns ID, birthDate, initState, ns, and numbered groups of From, To, transitionTime and transitionAge.

Deprecated

convertToWideFormat() is deprecated as of MicSim 4.0.0 and will be removed in a future release. It still works, and shows a message once per R session naming its replacement: use as_wide() on the result of simulate.micsim_model(). Set options(MicSim.show_deprecation = FALSE) to turn the message off.

See Also

micSim(), convertToLongFormat(), MicSim-concepts

Examples

stateSpace <- c("m", "f")
mortRates <- function(age, calTime) 0.00003 * exp(0.1 * age)
tm <- buildTransitionMatrix(NULL, cbind("dead", "mortRates"), stateSpace)

set.seed(1)
pop <- micSim(
  initPop = data.frame(ID = 1:10, birthDate = rep("19500101", 10),
                       initState = rep("f", 10),
                       stringsAsFactors = FALSE),
  transitionMatrix = tm, maxAge = 120,
  simHorizon = c(20000101, 20401231), verbose = FALSE
)
convertToWideFormat(pop)

# convertToWideFormat() is pipe-friendly:
pop |> convertToWideFormat()


MicSim documentation built on Dec. 2, 2026, 1:09 a.m.