How can I Change dimnames of matrices and data frames in R
Asked Answered
S

3

7

Let's say I have created the following matrix:

> x <- matrix(1:20000,nrow=100)
> x[1:10,1:10]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1  101  201  301  401  501  601  701  801   901
 [2,]    2  102  202  302  402  502  602  702  802   902
 [3,]    3  103  203  303  403  503  603  703  803   903
 [4,]    4  104  204  304  404  504  604  704  804   904
 [5,]    5  105  205  305  405  505  605  705  805   905
 [6,]    6  106  206  306  406  506  606  706  806   906
 [7,]    7  107  207  307  407  507  607  707  807   907
 [8,]    8  108  208  308  408  508  608  708  808   908
 [9,]    9  109  209  309  409  509  609  709  809   909
[10,]   10  110  210  310  410  510  610  710  810   910

What are the methods in R to change row and column names? For example, I like row names to be SS1, SS2, ..., SS100 and column names to be M1, M2, ..., M200. I usually work with data with 1000s of rows and columns, and I need a good method to do that. Some people use something like attributes(x)$dimnames <- list(...) and some use rownames <- paste(...). What are all possible methods?

My second question is, can I use the same methods after I convert the matrix to a data frame?

Smoothtongued answered 19/2, 2010 at 13:11 Comment(0)
P
13

From comment to answer:

row.names(x) <- paste("SS", 1:nrow(x), sep="")
colnames(x) <-  paste("M" , 1:ncol(x), sep="")

As @doug wrote, it works for matrices and data frames.

Parthenopaeus answered 19/2, 2010 at 14:16 Comment(0)
B
7

Yes same methods will work (matrix/data.frame)--see below:

A = matrix(1:12, nrow=4)
colnames(A) = c("col1", "col2", "col3")
row.names(A) = c("row1", "row2", "row3", "row4")

dfA = as.data.frame(A)
row.names(dfA) = c("r1", "r2", "r3", "r4")
colnames(A) = c("C1", "C2", "C3")

And to save time, you can do this:

x = rep("col", dim(M)[2])
y = 1:dim(M)[2]
colnames(M) = paste(x, y, sep="")
Bankbook answered 19/2, 2010 at 13:24 Comment(5)
OK but I have 1000s of rows&columns. I just can't type r1, r2, ... r1253, ... etc. There should be some automatic counter and pasting operations.Smoothtongued
row.names(A) <- paste("SS",1:nrow(A),sep="");colnames(A)<-paste("M",1:ncol(A),sep="")Parthenopaeus
@Marek: Could you please add this as an answer?Smoothtongued
edited my answer to include a snippet to automate row and col names creationBankbook
At the same time :) BTW you don't need to rep prefix ("col"), paste do it for you.Parthenopaeus
F
0

If it is within a list you can do.

 dimnames(x)[[1]]<-paste("SS", 1:nrow(x), sep="")
 dimnames(x)[[2]]<-paste("M" , 1:ncol(x), sep="")
Fergus answered 10/4, 2020 at 17:15 Comment(5)
sorry not very good with matrices, can you give an example of when the dimnames(matrix) will be a list?Earhart
It isn't that the dimnames are a list, is that the matrix is within a list. If the matrix is within a list you cannot use the row.names and colnames attributes. Also dimnames could be used for arrays of order greater than 2.Fergus
matrix_in_a_list = list(matrix(1:4,2,2,dimnames=list(c("a","b"),1:2))) ; dimnames(matrix_in_a_list) gives me NULL.. what happened?Earhart
matrix_in_a_list = list(matrix(1:4,2,2)) ; dimnames(matrix_in_a_list[[1]])<-list(c("a","b"),1:2)Fergus
You have to assign the dimnames to the matrix within the list not to the list itself. Also dimname is an attribute which needs to be directly referenced.Fergus

© 2022 - 2024 — McMap. All rights reserved.