Reshape from long to wide

Reshape a data frame from long form to wide form

longToWide( data, formula, sep="_")
The data frame.
A two-sided formula specifying measure variables and within-subject variables
Separator string used in wide-form variable names

The longToWide function is the companion function to wideToLong. The data argument is a "long form" data frame, in which each row corresponds to a single observation. The output is a "wide form" data frame, in which each row corresponds to a single experimental unit (e.g., a single subject).

The reshaping formula should list all of the measure variables on the left hand side, and all of the within-subject variables on the right hand side. All other variables are assumed to be between-subject variables. For example, if the accuracy of a participant's performance is measured at multiple time points, then the formula would be accuracy ~ time.

Multiple variables are supported on both sides of the formula. For example, suppose we measured the response time rt and accuracy of participants, across three separate days, and across three separate sessions within each day. In this case the formula would be rt + accuracy ~ days + sessions.


sep. In the example above where the reshaping formula was accuracy ~ time, if the default separator of sep="_" was used, and the levels of the time variable are t1, t2 and t3, then the output would include the variables accuracy_t1, accuracy_t2 and accuracy_t3.In the second example listed above, where the reshaping formula was rt + accuracy ~ days + sessions, the output variables would refer to levels of both within-subjects variables. For instance, rt_day1_session1, and accuracy_day2_session1 might be the names of two of the variables in the wide form data frame.


This package is under development, and has been released only due to teaching constraints. Until this notice disappears from the help files, you should assume that everything in the package is subject to change. Backwards compatibility is NOT guaranteed. Functions may be deleted in future versions and new syntax may be inconsistent with earlier versions. For the moment at least, this package should be treated with extreme caution.

See Also

wideToLong, reshape, stack, cast and melt (in the reshape package)

  • longToWide
library(lsr) long <- data.frame( id = c(1,2,3,1,2,3,1,2,3), time = c("t1","t1","t1","t2","t2","t2","t3","t3","t3"), accuracy = c(.50,.03,.72,.94,.63,.49,.78,.71,.16) ) longToWide( long, accuracy~time)
Documentation reproduced from package lsr, version 0.5, License: GPL-3

Community examples

Looks like there are no examples yet.