Find the unique rows in a matrix
uniquecombs.RdThis routine returns a matrix containing all the unique rows of the matrix supplied as its argument. That is, all the duplicate rows are stripped out. Note that the ordering of the rows on exit is not the same as on entry. It also returns an index attribute for relating the result back to the original matrix.
Details
Models with more parameters than unique combinations of covariates are not identifiable. This routine provides a means of evaluating the number of unique combinations of covariates in a model. The routine calls compiled C code.
Value
A matrix consisting of the unique rows of x (in arbitrary order).
The matrix has an "index" attribute. index[i] gives the row of the returned
matrix that contains row i of the original matrix.
See also
unique can do the same thing, including for
non-numeric matrices, but more slowly and without returning the
index.
Author
Simon N. Wood simon.wood@r-project.org
Examples
X<-matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1),6,3,byrow=TRUE)
print(X)
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 1 2 3
#> [3,] 4 5 6
#> [4,] 1 3 2
#> [5,] 4 5 6
#> [6,] 1 1 1
Xu <- uniquecombs(X);Xu
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 1 2 3
#> [3,] 1 3 2
#> [4,] 4 5 6
#> attr(,"index")
#> [1] 2 2 4 3 4 1
ind <- attr(Xu,"index")
## find the value for row 3 of the original from Xu
Xu[ind[3],];X[3,]
#> [1] 4 5 6
#> [1] 4 5 6