r - How can I sort a vector based on the indices contained in a different vector? -
i have vector sort based on indices contained in vector. instance, if have these vectors:
x <- c(0.4, 0.8, 0.1, 0.2) #<--values sorted y <- c(3,1,4,2)# <--indices base sorting vector y have distinct values 1 length of x (and therefore, both vectors have same number of elements)
the expected vector be:
0.8,0.2,0.4,0.1
or use order
x[order(y)] ## [1] 0.8 0.2 0.4 0.1
Comments
Post a Comment