Skip to contents

Convert sequence data from wide format (one row per sequence, columns as time points) to long format (one row per action).

Usage

wide_to_long(
  data,
  id_col = NULL,
  time_prefix = "V",
  action_col = "Action",
  time_col = "Time",
  drop_na = TRUE
)

Arguments

data

Data frame in wide format with sequences in rows.

id_col

Character. Name of the ID column, or NULL to auto-generate IDs. Default: NULL.

time_prefix

Character. Prefix for time point columns (e.g., "V" for V1, V2, ...). Default: "V".

action_col

Character. Name of the action column in output. Default: "Action".

time_col

Character. Name of the time column in output. Default: "Time".

drop_na

Logical. Whether to drop NA values. Default: TRUE.

Value

A data frame in long format, one row per (sequence, time point), sorted by identifier then time, with columns:

id

Sequence identifier. Named by id_col; when that is NULL the column is called id and holds the row number of the wide input (integer).

Time

Time point within the sequence (integer), taken from the numeric suffix of the wide column name. Named by time_col.

Action

The action/state at that time point. Named by action_col.

Any additional non-time columns from the original data are preserved and repeated on every row of their sequence.

Details

Converts wide sequence data (one row per sequence, one column per time point) to the long format used by many TNA functions and analyses.

See also

long_to_wide for the reverse conversion, prepare_for_tna for preparing data for TNA analysis.

Examples

wide_data <- data.frame(
  V1 = c("A", "B", "C"), V2 = c("B", "C", "A"), V3 = c("C", "A", "B")
)
long_data <- wide_to_long(wide_data)
head(long_data)
#>   id Time Action
#> 1  1    1      A
#> 2  1    2      B
#> 3  1    3      C
#> 4  2    1      B
#> 5  2    2      C
#> 6  2    3      A