Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

wideByFactor

Create multivariate data by a given factor


Description

wideByFactor modifies data.frame in such a way that variables are “separated” into several columns by factor levels.

Usage

wideByFactor(x, factor, common, sort=TRUE, keepFactor=TRUE)

Arguments

x

data frame

factor

character, column name of a factor by which variables will be divided

common

character, column names of (common) columns that should not be divided

sort

logical, sort resulting data frame by factor levels

keepFactor

logical, keep the ‘factor’ column

Details

Given data frame is modified in such a way, that output represents a data frame with c + f + n * v columns, where c is a number of common columns for all levels of a factor, f is a factor column, n is a number of levels in factor f and v is a number of variables that should be divided for each level of a factor. Number of rows stays the same!

Value

A data frame where divided variables have sort of “diagonalized” structure

Author(s)

Gregor Gorjanc

See Also

reshape in the stats package, melt and cast in the reshape package

Examples

n <- 10
f <- 2
tmp <- data.frame(y1=rnorm(n=n),
                  y2=rnorm(n=n),
                  f1=factor(rep(letters[1:f], n/2)),
                  f2=factor(c(rep(c("M"), n/2), rep(c("F"), n/2))),
                  c1=1:n,
                  c2=2*(1:n))

wideByFactor(x=tmp, factor="f1", common=c("c1", "c2", "f2"))
wideByFactor(x=tmp, factor="f1", common=c("c1", "c2"))

gdata

Various R Programming Tools for Data Manipulation

v2.18.0
GPL-2
Authors
Gregory R. Warnes, Ben Bolker, Gregor Gorjanc, Gabor Grothendieck, Ales Korosec, Thomas Lumley, Don MacQueen, Arni Magnusson, Jim Rogers, and others
Initial release
2017-06-05

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.