码迷,mamicode.com
首页 > 其他好文 > 详细

R matrix 转换为 dataframe

时间:2017-05-24 19:19:43      阅读:1580      评论:0      收藏:0      [点我收藏+]

标签:lis   tac   table   pip   stat   overflow   struct   use   mail   

When I try converting a matrix to a data frame, it works for me:

 > x <- matrix(1:6,ncol=2,dimnames=list(LETTERS[1:3],letters[24:25]))
 > data.frame(x)
   x y
A 1 4
B 2 5
C 3 6
 > str(data.frame(x))
`data.frame‘:   3 obs. of  2 variables:
  $ x: int  1 2 3
  $ y: int  4 5 6
 >

You can also use as.data.frame() to convert a matrix to a data.frame 
(but note that if colnames are missing form the matrix, as.data.frame() 
  constructs different colnames than does data.frame().


=========================================
> data <- c(0.1, 0.2, 0.3, 0.3, 0.4, 0.5)
> dimnames <- list(time=c(0, 0.5, 1), name=c("C_0", "C_1"))
> mat <- matrix(data, ncol=2, nrow=3, dimnames=dimnames)
> as.data.frame(as.table(mat))
  time name Freq
1    0  C_0  0.1
2  0.5  C_0  0.2
3    1  C_0  0.3
4    0  C_1  0.3
5  0.5  C_1  0.4
6    1  C_1  0.5
=========================================
REF:
https://stackoverflow.com/questions/15885111/create-data-frame-from-a-matrix-in-r
https://stat.ethz.ch/pipermail/r-help/2006-January/085978.html

 

R matrix 转换为 dataframe

标签:lis   tac   table   pip   stat   overflow   struct   use   mail   

原文地址:http://www.cnblogs.com/emanlee/p/6900409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!