0

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

lmo
  • 37,904
  • 9
  • 56
  • 69
ToBeGeek
  • 1,105
  • 4
  • 12
  • 20
  • Do you mean `rownames` and `colnames` instead of `row.names` and `col.names`? – Thomas Feb 28 '14 at 20:22
  • Yes, a kind of, I basically want to change the matrix's row names and column names @Thomas – ToBeGeek Feb 28 '14 at 20:22
  • @Thomas Do you know the answer ? please ? – ToBeGeek Feb 28 '14 at 20:50
  • 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 Feb 28 '14 at 20:53
  • I cannot upload data to this website... @BrodieG – ToBeGeek Feb 28 '14 at 21:22
  • just `dput(head(my.variable))` and post the results in the body of your question. See **[reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)** – BrodieG Feb 28 '14 at 21:24

1 Answers1

2

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.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • I changed that to %in%, but the order is not wrong now...@BrodieG – ToBeGeek Feb 28 '14 at 21:19
  • You need to use `match`. `%in%` is just a degenerate version of `match` that returns `TRUE` if a value in left operand is in right operand, but you need to know WHERE in the right operand the values from the left are, which you can do with `match`. Look at the documentation for `%in%`, which is also the documentation for `match` (`%in%` is implemented with `match`). – BrodieG Feb 28 '14 at 21:22
  • Can I have your E-mail please ? @BrodieG – ToBeGeek Feb 28 '14 at 21:33
  • @ToBeGeek, to the extent this answered your question, please consider checking it off as the answer. Thanks. – BrodieG Feb 28 '14 at 21:49