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 .
A5C1D2H2I1M1N2O1R2T1
source share