首页 > 代码库 > 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