Changing the order of vector in terms of anthother in R -
i have 2 vectors a
, b
.
=c(1, 3, 4, 5, 6, 2, 7) b =c(1.5, 5.5, 6.5, 8.5, 9.5, 0, 0) c=sort(a)
i not know how can create vector of result
in terms of c. how can vector in r?
result = c(1.5, 0, 5.5, 6.5, 8.5, 9.5, 0)
you don't want use vector c
in ordering of elements of b
, rather, want permute elements of b
in same way sort(a)
permutes elements of a
.
order
returns permutation. note a[order(a)]
same sort(a)
(with default decreasing = false
).
b[order(a)] [1] 1.5 0.0 5.5 6.5 8.5 9.5 0.0
Comments
Post a Comment