user39086
user39086

Reputation: 165

In R, how to produce an array containing sorted index of corresponding elements in another array?

In R, given an numeric array, how to produce another array which contains sorted index order into the original array? To clarify my question, consider an example: For an original array:

[33 11 55 22 44]

I want to produce another array:

[3 1 5 2 4]

Each element in the second array indicates sorted index of corresponding element in the first array.

In MATLAB, this can be done by using [B,XI]=sort(A); where XI is the wanted array.

Upvotes: 1

Views: 97

Answers (1)

Karan
Karan

Reputation: 284

Try the rank() function, found here.

Upvotes: 3

Related Questions