Reputation: 3
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,
Upvotes: 0
Views: 93
Reputation: 226547
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],]
Upvotes: 1