将矩阵转换为数据框:在一种情况下工作,而不是另一种情况 [英] Converting matrix to dataframe : Works in one case, not another

查看:227
本文介绍了将矩阵转换为数据框:在一种情况下工作,而不是另一种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个示例会话的输出剪辑。在其中,我创建一个矩阵,使用 matrix()函数,并将其简单地转换为数据框,其中 as.data.frame()函数。在第二部分中,我也创建一个矩阵,但是通过一个不同的过程(我想要做的工作),但即使 str()给我类似的输出,转换为数据帧时,我收到错误。任何想法?

Below is a snipped of the output of a sample session. In it, I create a matrix with the matrix() function and simply convert it to a dataframe with the as.data.frame() function. In the second section, I also create a matrix, but through a different process (the one I want to make work), but even though str() gives me analogous output, I get an error when converting to a dataframe. Any ideas?

编辑:最后,我添加了一行,我(重新)将矩阵转换为矩阵,然后将其转换为数据帧。它的作品,但是我不应该根据我在 str()输出的 test_mx 不能被转换为数据帧。所以我知道如何修复,但是我不明白为什么我需要做这个额外的步骤。

At the end, I added a line where I (re)cast the matrix to a matrix, and then convert that to a data frame. It works, but I shouldn't have to recast according to what I see in the str() output of the test_mx that fails to be cast as a data frame. So I know how to fix, but I don't understand why I need to do that extra step to do so.

R version 2.15.2 (2012-10-26) -- "Trick or Treat"

> library(reshape)
> ## This works
> ## ==========
> tmx = matrix(1:12*0.1, ncol=4)
> rownames(tmx) = c("A", "B", "C")
> colnames(tmx) = 0:3
> tmx
    0   1   2   3
A 0.1 0.4 0.7 1.0
B 0.2 0.5 0.8 1.1
C 0.3 0.6 0.9 1.2
> 
> str(tmx)
 num [1:3, 1:4] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:3] "A" "B" "C"
  ..$ : chr [1:4] "0" "1" "2" "3"
> as.data.frame(tmx)
    0   1   2   3
A 0.1 0.4 0.7 1.0
B 0.2 0.5 0.8 1.1
C 0.3 0.6 0.9 1.2
> 
> 
> 
> ## This does not
> ## =============
> t = 0:3
> thesd = 0.1
> dat = data.frame(
+     a1 = sin(2*pi*t/length(t)) + rnorm(t, sd=thesd),
+     b1 = sin(2*pi*t/length(t) - pi) + rnorm(t, sd=thesd),
+     c1 = sin(2*pi*t/length(t) - pi/2) + rnorm(t, sd=thesd),
+     t  = t
+ )
> 
> test_mx = cast(melt(dat, id.vars="t"), variable ~ t)
> tmp_rownames = as.character(test_mx[,1])
> test_mx = test_mx[,-1]
> tmp_colnames = colnames(test_mx)
> test_mx = as.matrix(test_mx)
> rownames(test_mx) = tmp_rownames
> colnames(test_mx) = tmp_colnames
> 
> str(test_mx)
 num [1:3, 1:4] 0.06211 -0.00596 -1.09718 1.1555 -0.96443 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:3] "a1" "b1" "c1"
  ..$ : chr [1:4] "0" "1" "2" "3"
> as.data.frame(test_mx)
Error in data.frame(rrownames(x), unx, check.names = FALSE) : 
  arguments imply differing number of rows: 0, 3
> 
> ## But this does work
> as.data.frame(as.matrix(test_mx))
             0           1           2           3
a1 -0.16166693  0.97479282  0.01471777 -1.01517539
b1 -0.01012797 -0.97745698 -0.12667287  0.96542412
c1 -1.07217297  0.06783235  1.12068282 -0.02012263

> ## why?


推荐答案

您应该使用 reshape2 ,而不是 reshape ,因为后者已经过时。

You should use reshape2, not reshape, as the latter is obsolete.

更改 cast by dcast acast

as.data.frame(test_mx)
            0           1            2           3
1 -0.08120468  0.97593052 -0.006127179 -1.15107784
2 -0.04165681 -1.02810193  0.004637454  0.99042403
3 -0.87862063  0.07346341  1.019113669 -0.01769976

这篇关于将矩阵转换为数据框:在一种情况下工作,而不是另一种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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