基于另一个矩阵的阶矩阵

时间:2014-11-26 11:41:32

标签: r

如果我有

ex1 <- 
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = c(10L, 12L
), .Dimnames = list(c("q1", "q2", "q3", "q4", "q5", "q6", "q7", 
"q8", "q9", "q10"), c("q1", "q2", "q3", "q4", "q5", "q6", "q7", 
"q8", "q9", "q10", "q11", "q12")))

ex2 <- 
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 
1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), .Dim = c(10L, 12L
), .Dimnames = list(c("q4", "q7", "q10", "q9", "q2", "q1", "q6", 
"q3", "q5", "q8"), c("q12", "q9", "q10", "q6", "q5", "q7", "q4", 
"q1", "q11", "q2", "q3", "q8")))

如何使ex2匹配ex1的行和列顺序相反

我尝试了this post中的方法,但无济于事。

1 个答案:

答案 0 :(得分:3)

只需使用rownamescolnames子集即可。 [非常灵活:

ex2[rownames(ex1),colnames(ex1)]
    q1 q2 q3 q4 q5 q6 q7 q8 q9 q10 q11 q12
q1   1  0  0  1  1  1  1  0  1   1   1   1
q2   0  1  0  1  1  1  1  0  1   1   0   1
q3   1  1  1  1  1  1  1  0  1   1   0   1
q4   0  0  0  1  1  0  0  0  0   0   0   1
q5   1  1  1  1  1  1  1  0  1   1   1   1
q6   1  0  0  1  1  1  1  0  1   1   1   1
q7   1  0  0  0  0  0  1  0  1   1   0   1
q8   1  1  1  1  1  1  1  1  1   1   1   1
q9   0  0  1  0  1  1  1  0  1   1   1   1
q10  1  0  0  0  0  1  0  1  1   0   1   1

ex1[rownames(ex2),colnames(ex2)]
    q12 q9 q10 q6 q5 q7 q4 q1 q11 q2 q3 q8
q4    0  1   0  1  1  0  1  1   1  1  1  0
q7    0  1   0  1  1  1  1  1   0  1  1  1
q10   1  1   1  1  1  1  1  1   1  1  1  1
q9    0  1   1  1  1  1  1  1   1  1  1  1
q2    0  0   0  1  0  0  0  1   0  1  1  1
q1    0  0   0  0  1  1  0  1   0  0  0  0
q6    0  1   0  1  1  1  1  1   0  1  1  1
q3    1  1   0  0  0  0  1  1   0  1  0  1
q5    0  0   1  1  1  1  1  1   0  1  1  0
q8    0  0   1  1  1  1  1  1   1  1  1  1