0
votes

I want to write a function to rename the matrix rows and columns.

My function is

changeMatrixName<- function(mat){
     nameRow<-vector(mode="character",length=nrow(mat))
     nameCol<-vector(mode="character",length=ncol(mat))
     nameCol<- colnames(mat)
     nameRow<- rownames(mat)
     #annotation[annotation$probeID==c("ILMN_1814092","ILMN_1668851"),]$symbol
     rowGeneName<-vector(mode="character",length=nrow(mat))
     colGeneName<-vector(mode="character",length=ncol(mat))


     rowGeneName<-annotation[annotation$probeID==c(nameRow),]$symbol
     colGeneName<-annotation[annotation$probeID==c(nameCol),]$symbol
     row.names(mat)<-rowGeneName
     col.names(mat)<-colGeneName
     return(mat)
  }

I have a test matrix like

      ILMN_1814092 ILMN_1805104 ILMN_2070570 ILMN_2232084 ILMN_1704579
 ILMN_1802380 4.972073e-03  0.016279737 0.0076933191 0.0214107369  0.001951975
 ILMN_1753196 2.222289e-04  0.080954797 0.0389565797 0.0220420297  0.002545084
 ILMN_1753830 1.657137e-05  0.009063726 0.0004676619 0.0008824427  0.007684124

When I run

test2<-changeMatrixName(test)

Error in rownames<-(x, value) : length of 'dimnames' [1] not equal to array extent In addition: Warning message: In annotation$probeID == c(nameRow) : longer object length is not a multiple of shorter object length

1
Do you mean rownames and colnames instead of row.names and col.names? - Thomas
Yes, a kind of, I basically want to change the matrix's row names and column names @Thomas - ToBeGeek
@Thomas Do you know the answer ? please ? - ToBeGeek
Take the time to read my answer below. I'm pretty sure I'm right about what you're doing wrong, though again, you'll get better answers if you provde data (including annotation). - BrodieG
I cannot upload data to this website... @BrodieG - ToBeGeek

1 Answers

2
votes

Hard to be sure without the data, but you almost certainly want something like:

rowGeneName <- annotation[match(nameRow, annotation$probeID),]$symbol
colGeneName <- annotation[match(nameCol, annotation$probeID),]$symbol

Right now, what you're using will overwrite your row and col vectors with a vector of length unlikely to be that of the original vectors. Look at expression:

annotation$probeID==c(nameRow)

There, you are going to take the values in nameRow, and compare them value by value with those of probeID, irrespective of the order there in. Those that happen to match will return TRUE, so you'll get a vector with however many matches you randomly happen to get by putting those two vectors side by side.

Another way to think about it, the expression above will only work as expected if probeID is of the exact same length as both nameRow and nameColumn, AND contains the exact same values in the same order as both nameRow and nameColumn, at which point you could just set rowGeneName and colGeneName directly equal to annotation$symbol.

match will actually search for the values of the first vector in the second, and return the index locations of the second vector that match.