Converts long-form or wide-form sequence data into useful formats for analysis, such as frequency tables, one-hot encodings, or edge lists (for network analysis).
Usage
convert_sequences(
data,
seq_cols = NULL,
id_col = NULL,
format = c("frequency", "onehot", "edgelist", "follows"),
na_values = NULL
)Arguments
- data
A data frame containing sequence data.
- seq_cols
Character vector of sequence column names. If NULL (default), auto-detects all columns except the ID column.
- id_col
Character. Name of the ID column. If NULL (default), uses the first column.
- format
Character. Output format:
- `"frequency"`
Counts of each action per sequence (default)
- `"onehot"`
Binary (1/0) presence of each action per sequence
- `"edgelist"`
From -> To transition pairs (for network analysis)
- `"follows"`
Action -> Previous Action pairs
- na_values
Character vector of values to treat as missing/NA. Default includes: NA, NaN, "", "*", " TraMineR typically uses "*" for missing states.
Value
A data frame structured according to the requested format:
`frequency`: Wide format with action counts per ID
`onehot`: Wide format with binary indicators per ID
`edgelist`: Long format with columns: id, from, to
`follows`: Long format with columns: id, act, follows
Examples
if (FALSE) { # \dontrun{
# Example sequence data
seq_data <- data.frame(
id = 1:3,
t1 = c("A", "B", "A"),
t2 = c("B", "A", "C"),
t3 = c("C", "C", "A"),
t4 = c("A", "*", "B") # "*" is TraMineR missing
)
# Frequency table
convert_sequences(seq_data, format = "frequency")
# One-hot encoding
convert_sequences(seq_data, format = "onehot")
# Edge list for network analysis
convert_sequences(seq_data, format = "edgelist")
# What follows what
convert_sequences(seq_data, format = "follows")
} # }