简体   繁体   English

使用带有矩阵或data.frame的融合提供不同的输出

[英]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: 如果我使用data.frame调用melt ,我会得到不同的结果:

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? 我可以使用data.frame获得第一个结果吗?

The basic reason is that there are different methods for melt , which you can see by running methods("melt") . 基本原因是有不同的melt methods ,您可以通过运行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. 其中大部分都可以通过reshape2:::melt.matrixreshape2:::melt.data.frame进行访问,它可以帮助您找出确切结果不同的原因。

But, to summarize what you will find, basically, melt.matrix will end up doing something like: 但是,总结一下你会发现什么,基本上, melt.matrix最终会做类似的事情:

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: ...而melt.data.frame最终会做这样的事情:

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. 需要注意的是data.frame方法不使用的rownames ,从而在评论中提到,以获取具有相同的结果data.frame方法,你就必须将它们添加到melt命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM