我有两个向量:
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
答案 0 :(得分:2)
使用order
,它返回sort
使用的排列:
vector2[order(vector1, decreasing=TRUE)]
[1] 0 1 0 0 0 1 0 0