A question was already asked on how keeping colnames in a matrix when applying apply
, sapply
, etc. here.
But I didn't find how to keep the column AND row names of a matrix.
Below an example:
mat = matrix(c(as.character(1:4)), nrow = 2)
colnames(mat) = c( 'col1', 'col2' )
rownames(mat) = c( 'row1', 'row2' )
mat = apply(mat, 2, function(x) as.numeric(paste(x)))
colnames(mat)
rownames(mat)
Thanks in advance :-)