This question is pretty unclear. I am only able to follow because you asked a similar question yesterday which I remember. Hopefully this answers your question.
First of all, you are creating your matrices incorrectly. What (I think) you actually want for four
and five
is this:
four<-matrix(c(6678,5677,6767,5555,1111,1112),3,2)
five<-matrix(c(5555,1112,1111,2222,1212,9999),3,2)
Your second problem can be solved like this:
matchOrder<-function(x,y){
y[match(x[,2],y[,1]),]
}
onea <- one[order(one[,2]),]
twoa <- matchOrder(onea,two)
threea <- matchOrder(twoa,three)
foura <- matchOrder(threea,four)
fivea <- matchOrder(foura,five)
finaltable <- cbind(onea,twoa,threea,foura,fivea)
finaltable
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2004 12345 12345 7777 7777 6789 NA NA NA NA
[2,] 2001 23456 23456 3456 3456 5677 5677 1111 1111 9999
[3,] 2002 23567 23567 2345 2345 6678 6678 5555 5555 2222
[4,] 2003 54321 54321 1234 1234 6767 6767 1112 1112 1212
In fact, what you really want to do is put these matrices in a list, and do this all with one call:
l <- list(two,three,four,five)
do.call(cbind,Reduce(matchOrder,l,onea,accumulate=T))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 2004 12345 12345 7777 7777 6789 NA NA NA NA
[2,] 2001 23456 23456 3456 3456 5677 5677 1111 1111 9999
[3,] 2002 23567 23567 2345 2345 6678 6678 5555 5555 2222
[4,] 2003 54321 54321 1234 1234 6767 6767 1112 1112 1212