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?