简体   繁体   English

如何在R中将矩阵转换为数据帧

[英]how do you convert matrix to data frame in R

I have a matrix like this: 我有一个像这样的矩阵:

x X

  [,1]   [,2]  
[1,] List,5 List,5
[2,] List,5 List,5
[3,] List,5 List,5
[4,] List,5 List,5
[5,] List,5 List,5
[6,] List,5 List,5

How would I convert this to a data frame? 我如何将其转换为数据框?

when I do this: 当我这样做时:

head(x[1,1],2)
[[1]]

               DATE_TIME                                                     SITE RESPONSE_TIME AVAIL_PERCENT AGENT
1   2013-MAY-24 05:09 PM  Keynote Business 40 TxP - User Time (seconds)[Geo Mean]             -             - 45693
2   2013-MAY-24 05:24 PM  Keynote Business 40 TxP - User Time (seconds)[Geo Mean]             -             - 45693
3   2013-MAY-24 05:39 PM  Keynote Business 40 TxP - User Time (seconds)[Geo Mean]             -             - 45693
4   2013-MAY-24 05:54 PM  Keynote Business 40 TxP - User Time (seconds)[Geo Mean]             -             - 45693
5   2013-MAY-24 06:09 PM  Keynote Business 40 TxP - User Time (seconds)[Geo Mean]             -             - 45693

The first thing to address is whether the location in that matrix of lists has any meaning. 首先要解决的是列表矩阵中的位置是否具有任何意义。 If you only want to "flatten" it out", you could just do this: 如果您只想“拉平”它,则可以这样做:

 # Not workig : out <- do.call( as.data.frame, c(x) )
 # Then try:

out <- do.call( rbind, as.list(x) )

If you want a record of the locations you will need to use an approach that labels the output, perhaps with apply or by processing one matrix column at a time. 如果要记录位置,则需要使用一种标记输出的方法,可能是apply或一次处理一个矩阵列。

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

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