首页 > 其他 > 详细

R matrix 转换为 dataframe

时间:2017-05-24 19:01:22      阅读:782      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!