Question

I am trying to rank the values of a row. I am trying to see which value is largest, which is second largest etc etc.

Here is a simple example:

test = c(0.005,0.007,0.009,-0.0008,0.5,-0.074)
order(test)
[1] 6 4 1 2 3 5
which.max(test)
[1] 5

The function which.max correctly gives me the column with the largest value, but it doesn't give me the second largest, third largest etc., etc.

I believed I could use the function order for this, but the output of that doesn't seem to be correct.

What am I doing wrong?

Was it helpful?

Solution

You just have to use the decreasing parameter:

> order(test,decreasing=T)
[1] 5 3 2 1 4 6

Or alternatively reverse it:

> rev(order(test))
[1] 5 3 2 1 4 6

OTHER TIPS

If you are looking for the values and not for the indices, then you should use sort

 sort(test,decreasing =TRUE)
    [1]  0.5000  0.0090  0.0070  0.0050 -0.0008 -0.0740
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top