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
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]