Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning a matrix after removing duplicate combinations of elements

I’m trying to obtain a matrix from which pairwise combinations of duplicates have been removed.

For example:

m <- cbind(c("A", "B"), c("B", "A"))

since BA is a duplicate from AB, I’m removing it from the matrix with

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

m[!duplicated(t(apply(m, 1, sort))), ]

which returns a vector with

"A" "B"

instead of a matrix with nrow of 1.

     [,1] [,2]
[1,] "A"  "B" 

If I use as.matrix(m), I’m getting

     [,1]
[1,] "A" 
[2,] "B" 

instead of

     [,1] [,2]
[1,] "A"  "B" 

This only happens with nrow = 1. In cases where more rows are returned, the result is kept as a matrix:

m <- cbind(c("A", "B", "A"), c("B", "A", "C"))
m[!duplicated(t(apply(m, 1, sort))), ]

which gives

     [,1] [,2]
[1,] "A"  "B" 
[2,] "A"  "C"  

How can I obtain a matrix, where only one row is returned. And what’s the reason that only a vector is returned and not a matrix in the example above?

>Solution :

The explanation to your question can be found in the help file, cp. help("["):

drop
relevant for matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.

The solution you are after:

m[!duplicated(t(apply(m, 1, sort))),, drop=FALSE]

Use

m[!duplicated(matrix(m[order(row(m), m)], ncol=ncol(m), byrow=TRUE)),, drop=FALSE]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading