wide2long: Convert Wide-Format Data to Long-Format Data

View source: R/wide2long.R

wide2longR Documentation

Convert Wide-Format Data to Long-Format Data

Description

Converts wide-format data to long-format data.

Usage

wide2long(x, y = NULL, grpnames=NULL)

Arguments

x

A data frame or matrix containing two columns for the observations in groups.

y

Optional; a vector containing data to combine with x. If NULL, the second column of x will be used as y.

grpnames

Optional character string vector for the names of groups, e.g. c("A","B").

Details

This function converts wide-format data to long format by combining two columns of data into a single column and creating a grouping variable to distinguish the original data sources.

Value

A data frame with two columns: Obs containing the combined values of x and y, and group indicating the original data source.

Author(s)

Zeynel Cebeci, A. Firat Ozdemir, Engin Yildiztepe

See Also

long2wide

Examples

 # Normal distributed groups of data with equal means and different variances
 set.seed(2)
 grp1 <- rnorm(20, 50, 5)
 grp2 <- rnorm(20, 50, 9)
 ds1 <- data.frame(A=grp1, B=grp2)
 head(ds1)
 bivarplot(ds1)

 # Convert to long data
 ds2 <- wide2long(ds1)
 head(ds2)

groupcompare documentation built on June 26, 2025, 1:08 a.m.