Convert data from univariate (stacked/tall/long) to multivariate (wide) form

No votes yet

The function and documentation is listed after the jump.

You may need to stretch out your browser window to see the whole thing.

# Description:
#	Some data is stored in univariate or stacked form, where each row contains data
#	from one individual per group, or in the case of logitudinal data, from
#	one observation per subject.  Sometimes, however, it is more convenient
#	when data is in a multivariate form, where each row contains data from all
#	individuals per group, or in the case of logitudinal data, from all
#	observations per subject.
# Usage:
#	make.multiv(univ, group_column, individual_column, variable_columns, sep=".")
# Arguments:
#	univ - The univariate data set; must have the same number of individuals per
#		group, for every group
#	group_column	- The column number (index) or name of the grouping id column
#	individual_column - The column number or name of ids of the individuals that
#		belong to the groups
#	variable_columns - A list of column numbers or names of the variables measured
#		for each individual in each group
#	sep - The seperator for the new variable column names that goes between the
#		variable name and the individual id
# Value:
#	Returns a dataset with all of the variables for each individual in each group
#		on a single row
# Author(s):
#	Jeff; updated at http://www.r-cookbook.com/node/59

make.multiv <- function(univ, group_column, individual_column, variable_columns, sep="."){ .by <- by(univ[variable_columns], univ[group_column], unlist) .vs <- t(sapply(.by, c)) .id <- data.frame(unique(univ[group_column]), row.names=NULL) .varpart <- rep(names(univ[variable_columns]), each=length(unlist(unique(univ[individual_column])))) .iidpart <- rep(unlist(unique(univ[individual_column])), length(variable_columns)) .names <- paste(.varpart, .iidpart, sep=sep) multiv <- cbind(.id, .vs) names(multiv) <- c(names(univ[group_column]), .names) multiv }

# Examples

univ1 <- read.table( textConnection(" FID TID TRAIT1 TRAIT2 1 1 4 5 1 2 9 10 2 1 2 3 2 2 2 4 3 1 11 12 3 2 0 1 "), header=TRUE)

make.multiv(univ1, 1, 2, c(3, 4), sep=".") make.multiv(univ1, "FID", "TID", c("TRAIT1", "TRAIT2"), sep=".")

# FID TRAIT1.1 TRAIT1.2 TRAIT2.1 TRAIT2.2 # 1 1 4 9 5 10 # 2 2 2 2 3 4 # 3 3 11 0 12 1

univ2 <- read.table( textConnection(" FID TID V1 V2 V3 1 1 4 5 6 1 2 9 10 5 2 1 2 3 4 2 2 2 4 4 3 1 11 12 1 3 2 0 1 0 "), header=TRUE)

make.multiv(univ2, 1, 2, c(3:5), sep="-TWIN") make.multiv(univ2, "FID", "TID", c("V1", "V2", "V3"), sep="-TWIN")

# FID V1-TWIN1 V1-TWIN2 V2-TWIN1 V2-TWIN2 V3-TWIN1 V3-TWIN2 # 1 1 4 9 5 10 6 5 # 2 2 2 2 3 4 4 4 # 3 3 11 0 12 1 1 0