Learn R Programming

lsr (version 1.0.0)

wideToLong: Reshape from wide to long

Description

Reshapes a data frame from wide form (one row per subject) to long form (one row per observation), using variable names to determine the structure.

Usage

wideToLong(data, within = "within", sep = "_", split = TRUE)

Value

A long-form data frame with one row per observation.

Arguments

data

A wide-form data frame with one row per subject (or experimental unit). Variables whose names contain sep are treated as repeated measures; all others are treated as between-subject variables.

within

A character string, or vector of strings, giving the name(s) to use for the within-subject factor column(s) in the output. Defaults to "within".

sep

The separator string used in the wide-form variable names to separate the measure name from the factor level(s). Defaults to "_". The separator must not appear anywhere else in the variable names.

split

Set to TRUE (the default) to split multiple within-subject factors into separate columns in the output. Set to FALSE to keep them combined into a single column.

Details

This function is the companion to longToWide. It determines the reshape structure from the variable names rather than requiring an explicit formula.

The naming scheme for repeated-measures variables places the measure name first, followed by the factor level(s), all joined by sep. For example, variables named accuracy_t1 and accuracy_t2 indicate a measure called accuracy recorded at two time points (t1 and t2). After reshaping, the long-form output contains one column called accuracy and a factor column (named by the within argument) with levels t1 and t2.

Designs with multiple within-subject factors are supported. For example, MRT_cond1_day1 encodes measure MRT at level cond1 of one factor and day1 of another. Supply within = c("condition", "day") to name both output columns. Multiple measured variables per observation (e.g., both MRT and PC) are also supported.

See Also

longToWide, reshape

Examples

Run this code
# simple design: accuracy measured at two time points for 4 participants
wide <- data.frame(
  id          = 1:4,
  accuracy_t1 = c(.15, .50, .78, .55),
  accuracy_t2 = c(.55, .32, .99, .60)
)
wideToLong(wide, "time")

# complex design: two measures (MRT, PC), two conditions, two days
wide2 <- data.frame(
  id             = 1:4,
  gender         = factor(c("male", "male", "female", "female")),
  MRT_cond1_day1 = c(415, 500, 478, 550),
  MRT_cond2_day1 = c(455, 532, 499, 602),
  MRT_cond1_day2 = c(400, 490, 468, 502),
  MRT_cond2_day2 = c(450, 518, 474, 588),
  PC_cond1_day1  = c(79, 83, 91, 75),
  PC_cond2_day1  = c(82, 86, 90, 78),
  PC_cond1_day2  = c(88, 92, 98, 89),
  PC_cond2_day2  = c(93, 97, 100, 95)
)

# default: condition and day become separate columns
wideToLong(wide2, within = c("condition", "day"))

# alternative: keep condition and day as one combined column
wideToLong(wide2, split = FALSE)

Run the code above in your browser using DataLab