2

In R, let M be the matrix

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    3    3
[3,]    2    4    5
[4,]    6    7    8

I would like to select the submatrix m

     [,1] [,2] [,3]
[1,]    1    3    3
[2,]    2    4    5
[3,]    6    7    8

using unique on M[,1], specifying to keep the row with the maximal value in the second columnM. At the end, the algorithm should keep row [2,] from the set \{[1,], [2,]\}. Unfortunately unique() returns me a vector with actual values, and not row numbers, after elimination of duplicates.

Is there a way to get the asnwer without the package plyr? Thanks a lot, Avitus

Avitus
  • 734
  • 2
  • 14
  • 27

3 Answers3

1

You're looking for duplicated.

m <- as.matrix(read.table(text="1    2    3
1    3    3
2    4    5
6    7    8"))
m <- m[order(m[,2], decreasing=TRUE), ]
m[!duplicated(m[,1]),]

#      V1 V2 V3
# [1,]  6  7  8
# [2,]  2  4  5
# [3,]  1  3  3
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
1

Here's how:

is.first.max <- function(x) seq_along(x) == which.max(x)

M[as.logical(ave(M[, 2], M[, 1], FUN = is.first.max)), ]
#      [,1] [,2] [,3]
# [1,]    1    3    3
# [2,]    2    4    5
# [3,]    6    7    8
flodel
  • 87,577
  • 21
  • 185
  • 223
1

Not the most efficient:

M <- matrix(c(1,1,2,6,2,3,4,7,3,3,5,8),4)

t(sapply(unique(M[,1]),function(i) {temp <- M[M[,1]==i,,drop=FALSE]
                                    temp[which.max(temp[,2]),]                                  
         }))

#     [,1] [,2] [,3]
#[1,]    1    3    3
#[2,]    2    4    5
#[3,]    6    7    8
Roland
  • 127,288
  • 10
  • 191
  • 288
  • I never used `sapply` before :-). Thanks for your code. It is interesting the use of `t()` at the very end, though. – Avitus May 22 '13 at 13:50