19
votes

I have read some references to similar problems here on SO, but haven't been able to find a solution yet and wondering if there is any way to do the following using just data.table.

I'll use a simplified example, but in practice, my data table has > 1000 columns similar to var1, var2, ... var1000, etc.

dt <- data.table(uid=c("a","b"), var1=c(1,2), var2=c(100,200))

I am looking for a solution that will allow me to get an output similar to reshape's melt function --

> melt(dt, id=c("uid"))
uid variable value
1   a     var1     1
2   b     var1     2
3   a     var2   100
4   b     var2   200

That is, all the columns except for uid are listed under a single column with the corresponding values in an adjoining column. I have tried this with a combination of list, etc, but might be missing something that is obvious.

All uids in dt are unique.

Thanks in advance.

3
I should add that using melt is not an option due to the time it takes to run the operation on a dataset that is several GBs in size.xbsd
Have you tried stack as an alternative to melt? Or perhaps unlist (but I think stack is faster).A5C1D2H2I1M1N2O1R2T1
See the answers at this question for some possible leads.A5C1D2H2I1M1N2O1R2T1
Yeah, that works ...cbind(stack(dt, select=-uid), enrolid=dt$uid)xbsd
I was thinking along the lines of dt[, stack(.SD), by = "uid"], assuming that everything else comprises .SDCols.A5C1D2H2I1M1N2O1R2T1

3 Answers

19
votes

For a data.table reshape, try the following:

dt[, list(variable = names(.SD), value = unlist(.SD, use.names = F)), by = uid]

The cost of the syntax is worth it; the function runs very quickly!

8
votes

stack generally outperforms melt.

A straightforward approach to this problem with stack would be:

dt[, stack(.SD), by = "uid"]

Of course, you can specify your .SDcols if necessary. And then, use setnames() to change the names to whatever you want.


(Self-promotion alert)

I wrote some functions and put them in a package called "splitstackshape". One of the functions is called Stacked(), and in the 1.2.0 version of the "splitstackshape" package, should work very fast.

It's a little bit different from just stacking all the remaining columns in a data.table. It is more analogous to base R's reshape() than melt() from "reshape2". Here's an example of Stacked() in action.

I've created a decently large data.table to do this test. There are 50 numeric columns we want to stack, and 50 factor columns we want to stack. I've also further optimized @Andreas's answer.

The data

set.seed(1)
m1 <- matrix(rnorm(10000*50), ncol = 50)
m2 <- matrix(sample(LETTERS, 10000*50, replace = TRUE), ncol = 50)
colnames(m1) <- paste("varA", sprintf("%02d", 1:50), sep = "_")
colnames(m2) <- paste("varB", sprintf("%02d", 1:50), sep = "_")
dt <- data.table(uid = 1:10000, m1, m2)

The functions for benchmarking

test1 <- function() Stacked(dt, "uid", c("varA", "varB"), "_")

## merged.stack
test2 <- function() merged.stack(dt, "uid", c("varA", "varB"), "_")

## unlist(..., use.names = TRUE) -- OPTIMIZED
test3 <- function() {
  list(cbind(dt[, "uid", with = FALSE], 
             dt[, list(variable = rep(names(.SD), each = nrow(dt)), 
                       value = unlist(.SD)), 
                .SDcols = 2:51]),
       cbind(dt[, "uid", with = FALSE], 
             dt[, list(variable = rep(names(.SD), each = nrow(dt)), 
                       value = unlist(.SD)), 
                .SDcols = 52:101]))
}

## unlist(..., use.names = FALSE) -- OPTIMIZED
test4 <- function() {
  list(cbind(dt[, "uid", with = FALSE], 
             dt[, list(variable = rep(names(.SD), each = nrow(dt)), 
                       value = unlist(.SD, use.names = FALSE)), 
                .SDcols = 2:51]),
       cbind(dt[, "uid", with = FALSE], 
             dt[, list(variable = rep(names(.SD), each = nrow(dt)), 
                       value = unlist(.SD, use.names = FALSE)), 
                .SDcols = 52:101]))
}

## Andreas's current answer
test5 <- function() {
  list(dt[, list(variable = names(.SD), 
                 value = unlist(.SD, use.names = FALSE)),
          by = uid, .SDcols = 2:51],
       dt[, list(variable = names(.SD), 
                 value = unlist(.SD, use.names = FALSE)), 
          by = uid, .SDcols = 52:101])
}

The results

library(microbenchmark)
microbenchmark(Stacked = test1(), merged.stack = test2(),
               unlist.namesT = test3(), unlist.namesF = test4(),
               AndreasAns = test5(), times = 3)
# Unit: milliseconds
#           expr        min         lq     median         uq        max neval
#        Stacked   391.3251   393.0976   394.8702   421.4185   447.9668     3
#   merged.stack   764.3071   769.6935   775.0799   867.2638   959.4477     3
#  unlist.namesT  1680.0610  1761.9701  1843.8791  1881.9722  1920.0653     3
#  unlist.namesF   215.0827   242.7748   270.4669   270.6944   270.9218     3
#     AndreasAns 16193.5084 16249.5797 16305.6510 16793.3832 17281.1154     3

^^ I'm not sure why Andreas's current answer is so slow here. The "optimization" I did was basically to unlist without using by, which made a huge difference on the "varB" (factor) columns.

The manual approach is still faster than the functions from "splitstackshape", but these are milliseconds we're talking about, and some pretty compact one-liner code!

Sample output

For reference, here is what the output of Stacked() looks like. It's a list of "stacked" data.tables, one list item for each stacked variable.

test1()
# $varA
#           uid .time_1       varA
#      1:     1      01 -0.6264538
#      2:     1      02 -0.8043316
#      3:     1      03  0.2353485
#      4:     1      04  0.6179223
#      5:     1      05 -0.2212571
#     ---                         
# 499996: 10000      46 -0.6859073
# 499997: 10000      47 -0.9763478
# 499998: 10000      48  0.6579464
# 499999: 10000      49  0.7741840
# 500000: 10000      50  0.5195232
# 
# $varB
#           uid .time_1 varB
#      1:     1      01    D
#      2:     1      02    A
#      3:     1      03    S
#      4:     1      04    L
#      5:     1      05    T
#     ---                   
# 499996: 10000      46    A
# 499997: 10000      47    W
# 499998: 10000      48    H
# 499999: 10000      49    U
# 500000: 10000      50    W

And, here is what the merged.stack output looks like. It's similar to what you would get when you use reshape(..., direction = "long") from base R.

test2()
#           uid .time_1       varA varB
#      1:     1      01 -0.6264538    D
#      2:     1      02 -0.8043316    A
#      3:     1      03  0.2353485    S
#      4:     1      04  0.6179223    L
#      5:     1      05 -0.2212571    T
#     ---                              
# 499996: 10000      46 -0.6859073    A
# 499997: 10000      47 -0.9763478    W
# 499998: 10000      48  0.6579464    H
# 499999: 10000      49  0.7741840    U
# 500000: 10000      50  0.5195232    W
6
votes

Shameless Self-promotion

You might want to try melt_ from my package Kmisc. melt_ is essentially a rewrite of reshape2:::melt.data.frame with most of the grunt work done in C, and avoids as much copying and type coercion as possible for a speedy implementation.

An example:

## devtools::install_github("Kmisc", "kevinushey")
library(Kmisc)
library(reshape2)
library(microbenchmark)
n <- 1E6
big_df <- data.frame( stringsAsFactors=FALSE,
  x=sample(letters, n, TRUE),
  y=sample(LETTERS, n, TRUE),
  za=rnorm(n),
  zb=rnorm(n),
  zc=rnorm(n)
)
all.equal(
  melt <- melt(big_df, id.vars=c('x', 'y')),
  melt_ <- melt_(big_df, id.vars=c('x', 'y'))
)
## we don't convert the 'variable' column to factor by default
## if we do, we see they're identical
melt_$variable <- factor(melt_$variable)
stopifnot( identical(melt, melt_) )
microbenchmark( times=5,
  melt=melt(big_df, id.vars=c('x', 'y')),
  melt_=melt_(big_df, id.vars=c('x', 'y'))
)

gives me

Unit: milliseconds
  expr       min        lq    median         uq       max neval
  melt 916.40436 931.60031 999.03877 1102.31090 1160.3598     5
 melt_  61.59921  78.08768  90.90615   94.52041  182.0879     5

With any luck, this will be fast enough for your data.