long2wide | R Documentation |
Converts long-format data to wide-format data by splitting based on groups.
long2wide(x)
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. |
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.
A data frame with columns corresponding to unique groups and rows containing the values for each group.
Zeynel Cebeci, A. Firat Ozdemir, Engin Yildiztepe
wide2long
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.