convMatr2df {wrMisc} | R Documentation |
Convert matrix (eg with redundant) row-names to data.frame
Description
This function provides flexible converting of matrix to data.frame.
For example repeated/redundant rownames are not allowed in data.frame(), thus the corresponding column-names have to be renamed using a counter-suffix.
In case of non-redundant rownames, a new column 'addIniNa' will be introduced at beginning to document the initial (redundant) rownames,
non-redundant rownames will be created.
Finally, this functions converts the corrected matrix to data.frame and checks/converts columns for transforming character to numeric if possible.
If the input is a data.frame containing factors, they will be converted to character before potential conversion.
Note: for simpler version (only text to numeric) see from this package .convertMatrToNum
.
Usage
convMatr2df(
mat,
addIniNa = TRUE,
duplTxtSep = "_",
silent = FALSE,
callFrom = NULL
)
Arguments
mat |
matrix (or data.frame) to be converted |
addIniNa |
(logical) if |
duplTxtSep |
(character) separator for enumerating replicated names |
silent |
(logical) suppres messages |
callFrom |
(character) allow easier tracking of message(s) produced |
Value
This functions returns a data.frame equivalent to the input matrix, an additional column named 'ID' will be added for initial rownames
See Also
numeric
, for simpler version (only text to numeric) see from this package .convertMatrToNum
Examples
dat1 <- matrix(1:10, ncol=2)
rownames(dat1) <- letters[c(1:3,2,5)]
## as.data.frame(dat1) ... would result in an error
convMatr2df(dat1)
df1 <- data.frame(a=as.character((1:3)/2), b=LETTERS[1:3], c=1:3)
str(convMatr2df(df1))
df2 <- df1; df2$b <- as.factor(df2$b)
str(convMatr2df(df2))