find unique elements in matrix based on a subset of columns

I have a table which i want to transform

     t LabelA LabelB start stop
 [1,]  1      4      9     1    2
 [2,]  1      3      5     1    2
 [3,]  1      1      6     1    2
 [4,]  2      4      9     1    2
 [5,]  2      3      5     1    2
 [6,]  2      1      6     1    2
 [7,]  2      7      2     2    2
 [8,]  3      3      5     3    4
 [9,]  3      1      6     3    4
[10,]  3      7      2     3    5
[11,]  3      4      9     3    5

I want to filter the data in a way that rows which just differ by there number in the first column are removed (not completely but only the duplicate). So for rows 1 and 4 only row 1 should remain in the table. Or for row 3 and 9 only row 9 should remain. It is important that the information in the first column is remained and that the earliest occurance of the row remaisn in the table not the other incidences.

Answers


You can use duplicated:

mat[!duplicated(as.data.frame(mat[, -1])), ]

      t LabelA LabelB start stop
[1,]  1      4      9     1    2
[2,]  1      3      5     1    2
[3,]  1      1      6     1    2
[7,]  2      7      2     2    2
[8,]  3      3      5     3    4
[9,]  3      1      6     3    4
[10,] 3      7      2     3    5
[11,] 3      4      9     3    5

where mat is the name of your matrix.


Try using duplicated function:

mymx <- matrix(c(1,4,9,1,2 ,1,3,5,1,2 ,1,1,6,1,2 ,2,4,9,1,2 ,2,3,5,1,2 ,2,1,6,1,2 ,2,7,2,2,2 ,3,3,5,3,4 ,3,1,6,3,4 ,3,7,2,3,5 ,3,4,9,3,5), ncol=5, byrow=T)
mymx[!duplicated(mymx[,-1]),]

> mymx[!duplicated(mymx[,-1]),]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    4    9    1    2
[2,]    1    3    5    1    2
[3,]    1    1    6    1    2
[4,]    2    7    2    2    2
[5,]    3    3    5    3    4
[6,]    3    1    6    3    4
[7,]    3    7    2    3    5
[8,]    3    4    9    3    5

Need Your Help

Best way to load plugins on windows that link to executable?

windows plugins dll mingw dlopen

I'm loading *.so plugins on Linux using dlopen, and I'd like to port the plugin functionality to windows with mingw. There is a dlfcn-win32 library that adds a dlopen API around native windows DLL