reshape_wide: Reshape a Long-Form Data Frame to Wide-Form

View source: R/reshape_wide.R

reshape_wideR Documentation

Reshape a Long-Form Data Frame to Wide-Form

Description

Takes the variables in a long-form data frame, widen, response, and ID, and transforms to a wide form data frame. All other variables are deleted in the transformed data frame. A simple wrapper for Base R reshape with sensible parameter names and sensible defaults, and able to specify a range of variables to transform. Conversion currently limited to converting based on a single grouping variable.

Usage

reshape_wide(data, widen, response, ID, prefix=NULL, sep="_", ...)

Arguments

data

Data frame that contains the variables to analyze as a wide-form single column.

widen

Name of the (single) grouping variable in the input long-form column.

response

Name of the variable of the response values in the input long-form column.

ID

Name of the ID field in the long-form column.

prefix

If TRUE, prefix the column names in the wide form of each corresponding level of the widen variable with the name of the response. Unless the values of widen are numeric, the default is FALSE, just using the level names as the column names.

sep

If prefix is TRUE, the separator between the name of the level and the name of the response variable, with default "_".

...

Older parameter values to be converted internally.

Details

Here is the correspondence between the original reshape parameter names and the reshape_wide parameter names.

reshape reshape_wide
----------- ----------------
v.names response
timevar widen
idvar ID
----------- ----------------

Author(s)

David W. Gerbing (Portland State University; gerbing@pdx.edu)

See Also

reshape.

Examples

d <- Read("Anova_rb")  # already in wide-form
dl <- reshape_long(d, sup1:sup4)  # convert to long-form

# convert back to wide form 
reshape_wide(dl, widen=Group, response=Response, ID=Person)

# with the name of the response prefixed to the column names
reshape_wide(dl, widen=Group, response=Response, ID=Person,
             prefix=TRUE, sep=".")

lessR documentation built on June 8, 2025, 10:35 a.m.