Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Ranking continuous data in decreasing order

$
0
0

I have a dataframe like this one:

set.seed(123)
df0 <- data.frame(x1 = rnorm(5, 0.1, 1), 
                  x2 = rnorm(5, 0.0, 1),
                  x3 = rnorm(5, 0.3, 1),
                  x4 = rnorm(5, 0.2, 1))
df0
          x1         x2         x3         x4
1 -0.4604756  1.7150650  1.5240818  1.9869131
2 -0.1301775  0.4609162  0.6598138  0.6978505
3  1.6587083 -1.2650612  0.7007715 -1.7666172
4  0.1705084 -0.6868529  0.4106827  0.9013559
5  0.2292877 -0.4456620 -0.2558411 -0.2727914

I'd like to rank the data in all columns by row. I know how to rank them in ascending order, i.e., with rank 1 assigned to smallest value, rank 2 for the second-smallest etc.:

df <- t(apply(df0, 1, rank))
df
     x1 x2 x3 x4
[1,]  1  3  2  4
[2,]  1  2  3  4
[3,]  4  2  3  1
[4,]  2  1  3  4
[5,]  4  1  3  2

But I don't know how to rank them in descending order. What's the trick?


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>