如何在R中将两个向量排序在一起?

时间:2013-12-15 20:31:10

标签: r

我有两个向量:

vector1<- c(0.01,0.02,0.04,0.5,0.9,0.002,0.07,0.008)
 vector2<- c(1,0,0,1,0,0,0,0)

vector2显示特定集合中vector 1的每个元素的成员资格。我想对vecotor1进行排序,但vector 2也应按w.r.t vector 1排序。如何在R中阻止它?

预期产出:

sort(vector1, decreasing=T)
[1] 0.900 0.500 0.070 0.040 0.020 0.010 0.008 0.002

>sorted_vector2:
 [1] 0 1 0 0 0 1 0 0

1 个答案:

答案 0 :(得分:2)

使用order,它返回sort使用的排列:

vector2[order(vector1, decreasing=TRUE)]
[1] 0 1 0 0 0 1 0 0