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

View source: R/long2wide.R

long2wideR Documentation

Convert Long-Format Data to Wide-Format Data

Description

Converts long-format data to wide-format data by splitting based on groups.

Usage

long2wide(x)

Arguments

x

A data frame or matrix with two columns. The first column must contain observations, and the second column must contain the group levels as factor.

Details

This function converts long-format data to wide format by splitting it based on unique groups in the second column. The resulting data frame has columns for each group, where each column contains the values of the first column for the corresponding group.

Value

A data frame with columns corresponding to unique groups and rows containing the values for each group.

Author(s)

Zeynel Cebeci, A. Firat Ozdemir, Engin Yildiztepe

See Also

wide2long

Examples

# Normal distributed groups of data with different means and different variances
set.seed(21)
obs1 <- rnorm(20, 50, 5)
obs2 <- rnorm(20, 55, 3)
obs <- c(obs1, obs2)
grp <- c(rep(as.factor("A"), 20), rep(as.factor("B"), 20))
ds1 <- data.frame(obs = obs, group = grp)
head(ds1)

# Reshape data to long format
ds2 <- long2wide(ds1)
head(ds2)
bivarplot(ds2)

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