0

I have a matrix:

       [,1] [,2]    [,3]
  [1,]   23   20 0.00835
  [2,]   17   28 0.02077
  [3,]    4   12 0.00600
  [4,]   11   13 0.00501
  [5,]    3   10 0.00653
  [6,]   19   22 0.00371
  [7,]    1    5 0.00492
  [8,]   27    2 0.00009
  [9,]   14   16 0.06497
 [10,]   23   20 0.00835
 [11,]   17   28 0.02077
 [12,]    4   12 0.00600
 [13,]    3   10 0.00653
 [14,]   19   22 0.00371
 [15,]    1    5 0.00492
 [16,]   27    2 0.00009
 [17,]   14   16 0.06497
 [18,]   11   13 0.00501
 [19,]   23   20 0.00835
 [20,]   17   28 0.02077
 [21,]    4   12 0.00600
 [22,]   11   13 0.00501
 [23,]    3   10 0.00653
 [24,]   19   22 0.00371
 [25,]    1    5 0.00492
 [26,]   27    2 0.00009
 [27,]   14   16 0.06497
 [28,]   23   20 0.00835
 [29,]    9   18 0.00509
 [30,]   17   28 0.02077
 [31,]    4   12 0.00600
 [32,]   11   13 0.00501
 [33,]    3   10 0.00653
 [34,]   19   22 0.00371
 [35,]    1    5 0.00492
 [36,]   27    2 0.00009
 [37,]   14   16 0.06497
 [38,]   23   20 0.00835
 [39,]    9   18 0.00509
 [40,]   17   28 0.02077
 [41,]    4   12 0.00600
 [42,]   11   13 0.00501
 [43,]    3   10 0.00653
 [44,]   19   22 0.00371
 [45,]    1    5 0.00492
 [46,]   27    2 0.00009
 [47,]   14   16 0.06497
 [48,]   23   20 0.00835
 [49,]    9   18 0.00509
 [50,]   17   28 0.02077
 [51,]    4   12 0.00600
 [52,]   11   13 0.00501
 [53,]    3   10 0.00653
 [54,]    1    5 0.00492

and I would like to get the indices of the rows which are unique. It would be important that the whole row is unique (so values in column 1,2,3). If the value in column 1 and 2 are the same for two rows but the value in column 3 is distinct it should be considered as unique and retained.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3419669
  • 293
  • 2
  • 4
  • 11

1 Answers1

2

It depends on what exactly you want. If you want to remove the duplicate rows from the matrix but leave one exemplar of every non-unique row in, the following will do the trick (Supposing that you matrix is named mat):

which(!duplicated(mat))

If, instead, you want only rows that are already unique in the original matrix, you want the following (hat tip to @Marek):

which(!duplicated(mat) & !duplicated(mat, fromLast=TRUE))
RoyalTS
  • 9,545
  • 12
  • 60
  • 101
  • This give you indices of duplicated element as well. As like in linked question you need to: `which(!duplicated(mat) & !duplicated(mat, fromLast=TRUE))`. – Marek Mar 28 '14 at 06:25
  • True, but it depends on what you want: Yours will return the indices of elements that occur exactly once (elements that are unique in the original matrix). Mine will return the indices of elements that occur exactly once _and_ a single index for every element that occurs more than once (such that if you use the indices on the matrix the resulting matrix rows will be unique). Good clarification. – RoyalTS Mar 28 '14 at 10:33
  • Question states "i would like to get the indices of the rows which are unique" so your answer doesn't quite fit. – Marek Mar 30 '14 at 20:50
  • You're right. I edited the answer such that the OP can decide for himself what exactly it is he wanted and find an answer to each of the two cases. – RoyalTS Mar 31 '14 at 12:36