I use reshape2
.
> x <- data.frame(A = 1:5, B = 55:51)
> library(reshape2)
> melt(x)
Using as id variables
variable value
1 A 1
2 A 2
3 A 3
4 A 4
5 A 5
6 B 55
7 B 54
8 B 53
9 B 52
10 B 51
It was interesting to see the benchmarks. melt
prints a message by default that we can turn off by being more explicit when calling a function.
> microbenchmark(stack(DF), melt(DF), times=100)
Unit: milliseconds
expr min lq median uq max neval
stack(DF) 122.3086 133.8435 139.6990 180.5338 250.9316 100
melt(DF) 140.0183 198.2025 227.8125 245.3444 367.1552 100
I find the difference small, and it gets smaller when printing for melt
is turned off. Looks like that my hunch of turning verbose mode off in my simulations may have helped.
> microbenchmark(stack(DF), melt(DF, measure.vars = names(DF)[grepl("X", names(DF))]), times=100)
Unit: milliseconds
expr min lq median uq max neval
stack(DF) 94.33681 124.9958 132.1747 144.7323 287.7438 100
melt(DF, measure.vars = names(DF)[grepl("X", names(DF))]) 99.44282 141.0594 150.2625 178.8081 249.0888 100