I would like to perform the rank of a column vector within a matrix.
I have the following matrix "data"
> data <- matrix (c(12,43,15,17,15,23,4,50,43,6,91,8), nrow = 4, ncol = 3)
With names and date it should look like this
Date Obs y1 y2 y3
2014 1 12 15 43
2014 2 43 23 6
2013 1 15 4 91
2013 2 17 50 8
output should be the rank of a variable against other variables, for each unique observation (date*Obs)
Date Obs formula(y1) formula(y2) formula(y3)
2014 1 3 2 1
2014 2 1 2 3
2013 1 2 3 1
2013 2 2 1 3
EDIT : Thanks to @Tim Hoolihan and user20650, for a simple matrix is :
t(apply(data, 1, function(x) rank(-x)))
Thank you for the help - from a lost noob.
Use apply to iterate over the rows and rank them:
> data <- matrix (c(12,43,15,17,15,23,4,50,43,6,91,8), nrow = 4, ncol = 3)
> t(apply(data, 1, function(x) rank(-x)))
[,1] [,2] [,3]
[1,] 3 2 1
[2,] 1 2 3
[3,] 2 3 1
[4,] 2 1 3