m2d {str2str} | R Documentation |
Matrix to Data-Frame
Description
m2d
converts a matrix to a data.frame. The benefit of m2d
over
as.data.frame.matrix
is that it provides the col
argument, which
allows the columns of the data.frame to be the columns of the matrix (i.e.,
col = 2
), the rows of the matrix (i.e., col = 1
), or the expanded
matrix (i.e., col = 0
).
Usage
m2d(m, col = 2, stringsAsFactors = FALSE, check = TRUE)
Arguments
m |
matrix |
col |
numeric vector of length 1 that is equal to either 0, 1, or 2. |
stringsAsFactors |
logical vector of length 1 specifying whether any resulting
character columns in the return object should be factors. If |
check |
logical vector of length 1 specifying whether to check the structure
of the input arguments. For example, check whether |
Value
data.frame with rownames and colnames specified by dimnames(m)
and col
. If col = 0
, then the rownames are default (i.e., "1","2","3", etc.)
and the colnames are the following: the first two columns are names(dimnames(m))
(if NULL they are "rownames" and "colnames", respectively) and the third is
"element".
Examples
mtcars2 <- as.matrix(mtcars, rownames.force = TRUE) # to make sure dimnames stay in the example
m2d(mtcars2) # default
m2d(m = mtcars2, col = 1) # data.frame columns are matrix rownames
m2d(m = mtcars2, col = 0) # data.frame columns are the entire matrix
mat <- cbind(lower = letters, upper = LETTERS)
m2d(mat)
m2d(mat, stringsAsFactors = TRUE)
m2d(mat, col = 0)
m2d(mat, col = 0, stringsAsFactors = TRUE)