Reputation: 11
I have searched google a lot, and there were many useful R code solutions in Stackoverflow. I have two matrices, they are index matrix and real return matrix. Their dimensions are same (about 500 by 500 matrix?) They were too big, so I am doing exercise with much smaller example.
> setwd("B:/FE/2013.4.28")
> data <- read.csv("ex.csv")
> data
a b c d e f g h i
1 7 5 2 1 11 4 5 55 22
2 3 1 3 5 2 4 6 8 13
3 90 99 999 9999 2 22 222 2223 10973
4 8 4 988 1004 6 15 12 78 50
> id <- t(apply(data,1,order))
> lapply(1:nrow(id),function(i)data[i,id[i,]])
[[1]]
d c f b g a e i h
1 1 2 4 5 5 7 11 22 55
[[2]]
b e a c f d g h i
2 1 2 3 3 4 5 6 8 13
[[3]]
e f a b g c h d i
3 2 22 90 99 222 999 2223 9999 10973
[[4]]
b e a g f i h c d
4 4 6 8 12 15 50 78 988 1004
> matrix(names(data)[id],ncol=ncol(data))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "d" "c" "f" "b" "g" "a" "e" "i" "h"
[2,] "b" "e" "a" "c" "f" "d" "g" "h" "i"
[3,] "e" "f" "a" "b" "g" "c" "h" "d" "i"
[4,] "b" "e" "a" "g" "f" "i" "h" "c" "d"
> criteria <- matrix(names(data)[id],ncol=ncol(data))
> data2 <- read.csv("ex2.csv",header=TRUE)
> data2
a b c d e f g h i
1 1.100000 1.13000 0.900000 1.70000 1.54500 1.220000 2.000000 1.40000 1.9800000
2 1.242300 1.64345 1.452500 2.20000 1.43240 0.234423 1.556234 1.32432 1.2342300
3 1.542542 1.35432 1.342523 1.23432 1.43254 1.324320 1.546540 2.43200 0.4321432
4 1.542354 1.65460 0.324130 0.65460 1.23452 1.654325 1.342134 0.34124 1.1000000
> rr <- order(data2,criteria)
> matrix(rr,ncol=ncol(criteria))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 22 16 5 20 28 18 3 6 33
[2,] 12 9 21 2 11 19 17 24 25
[3,] 32 1 34 23 7 10 27 8 14
[4,] 35 36 15 30 29 4 26 13 31
This was my code. I have set 'ex' as index matrix, and ex2 as real return matrix. I wanted to re-order 'ex' ascending row by row (each row means the index(criteria) number in one week)
Then, my definite goal is re-ordering 'matrix ex2' as 'matrix ex'
I had copied and pasted lapply code in stackoverflow. So I could re-order 'matrix ex' like this.
> matrix(names(data)[id],ncol=ncol(data))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "d" "c" "f" "b" "g" "a" "e" "i" "h"
[2,] "b" "e" "a" "c" "f" "d" "g" "h" "i"
[3,] "e" "f" "a" "b" "g" "c" "h" "d" "i"
[4,] "b" "e" "a" "g" "f" "i" "h" "c" "d"
> rr <- order(data2,criteria)
> matrix(rr,ncol=ncol(criteria))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 22 16 5 20 28 18 3 6 33
[2,] 12 9 21 2 11 19 17 24 25
[3,] 32 1 34 23 7 10 27 8 14
[4,] 35 36 15 30 29 4 26 13 31
data was ex, data2 was ex2. d was header of smallest number in row 1(=1), h was header of largest number in row 1(=55) Therefore, I have succeeded at making ex ordered ascending, row by row. ex was re-ordered row by row. It was presented by headers. I could do this with copy-and paste the code on Stackoverflow.
So, I want to re-order ex2 using the order of ex. Then, I could get only this results. Function 'order' shows me that 22nd number in matrix ex2 was smallest in ex2.
But, I have two problem. First problem is that "I want to re-order ex2 on ex, row by row". My result was ordered, but was not ordered row by row, Second problem was that " I want to know the numeric value of ex2, not the rank", My result was showing me the rank of matrix elements.
How can I get ordered ex2 as the order of ex, using row by row method?
I'm not good at English, I'm sorry for my poor English. Thanks for reading my question!
Upvotes: 1
Views: 200
Reputation: 7130
If you want to order
the rows of ex2
according to rows of ex
,
idx = t(apply(ex, 1, order))
t(sapply(1:nrow(idx), function(i) ex2[i,][idx[i,]]))
Upvotes: 1