data.table (version 1.9.4)

melt.data.table: Fast melt for data.table

Description

A melt.data.table S3 method extending reshape2:::melt, for melting a data.table. reshape2 also has to be loaded for using melt.data.table. A lot similar to reshape2:::melt.data.frame , but much faster and with some additional features.

Usage

## fast melt a data.table
## S3 method for class 'data.table':
melt(data, id.vars, measure.vars, 
	variable.name = "variable", value.name = "value", 
	..., na.rm = FALSE, variable.factor = TRUE, 
	value.factor = FALSE, 
	verbose = getOption("datatable.verbose"))

Arguments

data
A data.table object to melt.
id.vars
vector of id variables. Can be integer (corresponding id column numbers) or character (id column names) vector. If missing, all non-measure columns will be assigned to it.
measure.vars
vector of measure variables. Can be integer (corresponding measue column numbers) or character (measure column names) vector. If missing, all non-id columns will be assigned to it.
variable.name
name for the measured variable names column. The default name is 'variable'.
value.name
name for the molten data values column. The default name is 'value'.
na.rm
If TRUE, NA values will be removed from the molten data.
variable.factor
If TRUE, the variable column will be converted to factor, else it will be a character column.
value.factor
If TRUE, the value column will be converted to factor, else the molten value type is left unchanged.
verbose
TRUE turns on status and information messages to the console. Turn this on by default using options(datatable.verbose=TRUE). The quantity and types of verbosity may be expanded in future.
...
any other arguments to be passed to/from other methods

Value

  • An unkeyed data.table containing the molten data.

Details

If id.vars and measure.vars are both missing, all non-numeric/integer/logical columns are assigned as id variables and the rest of the columns are assigned as measure variables. If only one of id.vars or measure.vars is supplied, the rest of the columns will be assigned to the other. Both id.vars and measure.vars can have the same column more than once and same column can be as id and measure variables.

melt.data.table also accepts list columns for both id and measure variables. When all measure.vars are not of the same type, they'll be coerced according to the hierarchy list > character > numeric > integer > logical. For example, any of the measure variables is a list, then entire value column will be coerced to a list. Note that, if the type of value column is a list, na.rm = TRUE will have no effect.

All class attributes on value column (example: Date) are dropped silently.

See Also

dcast.data.table, https://r-forge.r-project.org/projects/datatable/

Examples

Run this code
set.seed(45)
require(reshape2)
require(data.table)
DT <- data.table(
      i1 = c(1:5, NA), 
      i2 = c(NA,6,7,8,9,10), 
      f1 = factor(sample(c(letters[1:3], NA), 6, TRUE)), 
      c1 = sample(c(letters[1:3], NA), 6, TRUE), 
      d1 = as.Date(c(1:3,NA,4:5), origin="2013-09-01"), 
      d2 = as.Date(6:1, origin="2012-01-01"))
DT[, l1 := DT[, list(c=list(rep(i1, sample(5,1)))), by = i1]$c] # list cols
DT[, l2 := DT[, list(c=list(rep(c1, sample(5,1)))), by = i1]$c]

# basic examples
melt(DT, id=1:2, measure=3) 
melt(DT, id=c("i1", "i2"), measure="f1", value.factor=TRUE) # same as above, but value is factor

# on Date
melt(DT, id=c("i1", "f1"), measure=c("d1", "d2")) # date class attribute lost
melt(DT, id=c("i1", "f1"), measure=c("c1", "d1")) # value is char, date attribute lost

# on list
melt(DT, id=1, measure=c("l1", "l2")) # value is a list
melt(DT, id=1, measure=c("c1", "l1")) # c1 coerced to list

# on character
melt(DT, id=1, measure=c("c1", "f1")) # value is char
melt(DT, id=1, measure=c("c1", "i2")) # i2 coerced to char

# on na.rm=TRUE
melt(DT, id=1, measure=c("c1", "i2"), na.rm=TRUE) # remove NA

Run the code above in your browser using DataLab