1

I have a matrix like this:

   10 0.646519601   0   0.085929349 0.255477717 0.012073334
   40 0.138403594   0   0.065979943 0.776693742 0.018922721

I want to keep the first column, and get rest numbers sorted by row. The result should be like this:

   10 5 1 2 4 3  #0.64 is the largest; 0 is the smallest in the row#
   40 4 1 3 5 2
1

Apply the rank function row-wise, e.g.

dat <- read.table(textConnection("
10 0.646519601   0   0.085929349 0.255477717 0.012073334
40 0.138403594   0   0.065979943 0.776693742 0.018922721
"))

R> cbind(dat[, 1], t(apply(dat[, -1], 1, rank)))
        V2 V3 V4 V5 V6
[1,] 10  5  1  3  4  2
[2,] 40  4  1  3  5  2
  • Thanks. Two more concerns. 1. What does the "t" mean? a new matrix? 2. The first "1" means the first column; the second "1" means "row-wise". Right? – Apple Aug 27 '13 at 16:32
  • 1
    1. t returns the transposed matrix (see ?t in R); 2. correct – rcs Aug 27 '13 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.