melt() is reshaping wide-to-long, notb unlike
tidyr::pivot_longer()` for
data.trame objects. See data.table::melt()
for explanations.
melt(data, ..., na.rm = FALSE, value.name = "value")
# S3 method for class 'data.trame'
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")
)
A data.trame object.
Arguments passed to other methods.
Should NA
values be removed? Default is FALSE
.
Name for the molten data values column(s).
Vector of id
variables.
Measure variables for melting. Can be missing.
Name (default variable
) of output column containing
the information about melted columns.
If TRUE
, the variable column is converted to a
factor, otherwise, it is a character column.
If TRUE
, the value column is converted to a factor,
else, it is left unchanged.
TRUE
turns on status and information messages.
An unkeyed data.trame containing the molten data.
# Adapted from first example of ?melt.data.table
set.seed(45)
library(data.trame)
dtrm <- data.trame(
i_1 = c(1:5, NA),
n_1 = c(NA, 6, 7, 8, 9, 10),
f_1 = factor(sample(c(letters[1:3], NA), 6L, TRUE)),
f_2 = ordered(c("z", "a", "x", "c", "x", "x")),
c_1 = sample(c(letters[1:3], NA), 6L, TRUE),
c_2 = sample(c(LETTERS[1:2], NA), 6L, TRUE),
d_1 = as.Date(c(1:3,NA,4:5), origin = "2013-09-01"),
d_2 = as.Date(6:1, origin = "2012-01-01")
)
# add a couple of list cols
dtrm$l_1 <- dtrm[, ~list(c = list(rep(i_1, sample(5, 1L)))), by = ~i_1]$c
dtrm$l_2 <- dtrm[, ~list(c = list(rep(c_1, sample(5, 1L)))), by = ~i_1]$c
# id.vars, measure.vars as character/integer/numeric vectors
melt(dtrm, id.vars = 1:2, measure.vars = "f_1")
#> # A data.trame: [6 × 4]
#> i_1 n_1 variable value
#> <int> <dbl> <fct> <chr>
#> 1 1 NA f_1 a
#> 2 2 6 f_1 c
#> 3 3 7 f_1 b
#> 4 4 8 f_1 NA
#> 5 5 9 f_1 c
#> 6 NA 10 f_1 b