Using a melt with a matrix or data.frame gives a different output

Consider the following code:

set.seed(1) M = matrix(rnorm(9), ncol = 3) dimnames(M) = list(LETTERS[1:3], LETTERS[1:3]) print(M) ABC A -0.6264538 1.5952808 0.4874291 B 0.1836433 0.3295078 0.7383247 C -0.8356286 -0.8204684 0.5757814 melt(M) Var1 Var2 value 1 AA -0.6264538 2 BA 0.1836433 3 CA -0.8356286 4 AB 1.5952808 5 BB 0.3295078 6 CB -0.8204684 7 AC 0.4874291 8 BC 0.7383247 9 CC 0.5757814 

If I call melt using data.frame , I get a different result:

 DF = data.frame(M) melt(DF) variable value 1 A -0.6264538 2 A 0.1836433 3 A -0.8356286 4 B 1.5952808 5 B 0.3295078 6 B -0.8204684 7 C 0.4874291 8 C 0.7383247 9 C 0.5757814 

I found the docs a bit confusing, so can anyone help me understand this behavior? Can I get the first result using data.frame?

+6
matrix r dataframe reshape2 melt
source share
1 answer

The main reason is that there are different methods for melt that you can see by running methods("melt") . Most of them can be accessed by, say, reshape2:::melt.matrix or reshape2:::melt.data.frame , which can send you on a quest to find out why the results are different.

But, to summarize what you find, basically melt.matrix will eventually do something like:

 cbind(expand.grid(dimnames(M)), value = as.vector(M)) # Var1 Var2 value # 1 AA -0.6264538 # 2 BA 0.1836433 # 3 CA -0.8356286 # 4 AB 1.5952808 # 5 BB 0.3295078 # 6 CB -0.8204684 # 7 AC 0.4874291 # 8 BC 0.7383247 # 9 CC 0.5757814 

... while melt.data.frame will eventually do something like this:

 N <- data.frame(M) data.frame(var1 = rep(names(N), each = nrow(N)), value = unlist(unname(N))) # var1 value # 1 A -0.6264538 # 2 A 0.1836433 # 3 A -0.8356286 # 4 B 1.5952808 # 5 B 0.3295078 # 6 B -0.8204684 # 7 C 0.4874291 # 8 C 0.7383247 # 9 C 0.5757814 

Of course, the actual functions perform much larger error checking and are designed to conveniently indicate which columns should be melted, etc.

Note that the data.frame method data.frame not use rownames , as indicated in the comments, in order to get the same result using the data.frame method, you will have to add them to melt .

+11
source share

All Articles