Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

r - Using melt with matrix or data.frame gives 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)
           A          B         C
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    A    A -0.6264538
2    B    A  0.1836433
3    C    A -0.8356286
4    A    B  1.5952808
5    B    B  0.3295078
6    C    B -0.8204684
7    A    C  0.4874291
8    B    C  0.7383247
9    C    C  0.5757814

If i call melt using a 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 little bit confusing on this, so anyone can help me understand this behavior? Can i get the first result using a data.frame?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The basic reason is that there are different methods for melt, which you can see by running methods("melt"). Most of these can be accessed by, say, reshape2:::melt.matrix or reshape2:::melt.data.frame, which can send you on your hunt for figuring out exactly why the results are different.

But, to summarize what you will find, basically, melt.matrix will end up doing something like:

cbind(expand.grid(dimnames(M)), value = as.vector(M))
#   Var1 Var2      value
# 1    A    A -0.6264538
# 2    B    A  0.1836433
# 3    C    A -0.8356286
# 4    A    B  1.5952808
# 5    B    B  0.3295078
# 6    C    B -0.8204684
# 7    A    C  0.4874291
# 8    B    C  0.7383247
# 9    C    C  0.5757814

... while melt.data.frame will end up doing 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 do a lot more error checking and are designed to let you conveniently specify which columns should be melted and so on.

Note that the data.frame method doesn't make use of the rownames, so as mentioned in the comments, to get the same result with the data.frame method, you'll have to add them in to the melt command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...