Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivot on data.table similar to rehape melt function

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.

like image 393
xbsd Avatar asked Aug 26 '13 17:08

xbsd


People also ask

How do you get the melt function in R?

The melt function is to be found in the reshape package. If you do not have that package installed, then you will need to install it with install. packages("reshape") before you can use it. Then, when the package is installed, make it available with library(reshape) .

What does setDT do in R?

The setDT function takes care of this issue by allowing to convert lists - both named and unnamed lists and data. frames by reference instead. That is, the input object is modified in place, no copy is being made.

What is melt in Rstudio?

The melt() function in R programming is an in-built function. It enables us to reshape and elongate the data frames in a user-defined manner. It organizes the data values in a long data frame format.


1 Answers

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!

like image 163
Andreas Avatar answered Oct 05 '22 13:10

Andreas