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
longToWide function is the companion function to
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
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
t3, then the output would include the variables
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,
accuracy_day2_session1might 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.
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)