0

I would like to get corresponding names for a vector in Matrix1 from another matrix M2.

For example, one matrix=M1 has only names, A, B, C , D ,,,,etc 100 * 1 And other matrix=M2 also has those names, but resorted randomly in such names, and has one more column, M2 size is 100 *2.

For now, i would like to get corresponding information of second column to be matched with names of M1. my matrix is too large, without using forloop, is there any way to get it quickly?

many thanks,

jessica
  • 3
  • 1
  • 1
    Hi, welcome to StackOverflow! Your description of the problem is far too vague for us to provide much help. Try reading [here](http://stackoverflow.com/q/5963269/324364) about how to produce a good example illustrating your problem and come back and edit your question accordingly. – joran Jul 26 '11 at 20:45

1 Answers1

1

I'm guessing that you want something like this:

M1 <- matrix(LETTERS,ncol=1)
## use data.frame rather than matrix to preserve numeric values in column 2
M2 <- data.frame(sample(LETTERS),1:26)
M2[match(M1[,1],M2[,1]),]

or M2[match(M1[,1],M2[,1]),2] if you just want the numeric values.

This is even easier if you maintain the names as row names:

M2 <- data.frame(1:26,row.names=sample(LETTERS))
M2[M1[,1],]
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • glad I could help. Do read @joran's link about reproducible examples though. It's easier when we don't have to guess ... If possible, you should go ahead and edit your question now (even though you have an answer) to make it clearer for future readers. – Ben Bolker Jul 26 '11 at 21:12