Learn R Programming

wrMisc (version 1.2.3)

convMatr2df: Convert matrix (eg with redundant) row-names to data.frame

Description

convMatr2df 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 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 TRUE an additional column ('ID') with rownames will be added at beginning

duplTxtSep

(character) separator for enumerating replicated names

silent

(logical) suppres messages

callFrom

(character) allow easier tracking of message(s) produced

Value

data.frame

See Also

for simpler version (only text to numeric) see from this package .convertMatrToNum

Examples

Run this code
# NOT RUN {
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)
convMatr2df(data.frame(a=as.character((1:3)/2),b=LETTERS[1:3],c=1:3))
tmp <- data.frame(a=as.character((1:3)/2),b=LETTERS[1:3],c=1:3,stringsAsFactors=FALSE)
convMatr2df(tmp)
tmp <- data.frame(a=as.character((1:3)/2),b=1:3,stringsAsFactors=FALSE)
convMatr2df(tmp) 
# }

Run the code above in your browser using DataLab