Learn R Programming

Kmisc (version 0.5.0)

melt_: Make a 'Wide' data set 'Long'

Description

Inspired by reshape2:::melt, we melt data.frames and matrixs. This function is built for speed.

Usage

melt_(data, ...)
"melt_"(data, id.vars, measure.vars, variable.name = "variable", ..., value.name = "value")
"melt_"(data, ...)

Arguments

data
The data.frame to melt.
...
Arguments passed to other methods.
id.vars
Vector of id variables. Can be integer (variable position) or string (variable name). If blank, we use all variables not in measure.vars.
measure.vars
Vector of measured variables. Can be integer (variable position) or string (variable name). If blank, we use all variables not in id.vars.
variable.name
Name of variable used to store measured variable names.
value.name
Name of variable used to store values.

Details

If items to be stacked are not of the same internal type, they will be promoted in the order logical > integer > numeric > character.

Examples

Run this code
n <- 20
tmp <- data.frame( stringsAsFactors=FALSE,
  x=sample(letters, n, TRUE),
  y=sample(LETTERS, n, TRUE),
  za=rnorm(n),
  zb=rnorm(n),
  zc=rnorm(n)
)

stopifnot(
  identical(
    melt_(tmp, id.vars=c('x', 'y')),
    melt_(tmp, measure.vars=c('za', 'zb', 'zc'))
  )
)

Run the code above in your browser using DataLab