使用熔体与矩阵或data.frame给出不同的输出 [英] Using melt with matrix or data.frame gives different output

查看:165
本文介绍了使用熔体与矩阵或data.frame给出不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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

如果我打电话使用 data.frame 融化,我得到不同的结果:

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


$ b $我发现文档有点混乱,所以任何人都可以帮我理解这个行为?我可以使用data.frame获得第一个结果吗?

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?

推荐答案

基本原因是有不同的 ,您可以通过运行方法(melt)来查看 。大多数这些可以被访问,例如, reshape2 ::: melt.matrix reshape2 ::: melt.data.frame ,它可以发送你的狩猎,以确定为什么结果是不同的。但是,总而言之,您将会发现,基本上, melt.matrix 将最终做的事情如下:

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.

/ p>

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

...而 melt.data.frame 最终会这样做:

... 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.

请注意, data.frame 方法不会使用 rownames ,如在评论中提到的,要获得与 data.frame 方法相同的结果,你会必须将它们添加到 fusion 命令中。

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给出不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆