首页 > 代码库 > R matrix 转换为 dataframe
R matrix 转换为 dataframe
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 yA 1 4B 2 5C 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 Freq1 0 C_0 0.12 0.5 C_0 0.23 1 C_0 0.34 0 C_1 0.35 0.5 C_1 0.46 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。