This function takes a matrix as input and returns a new matrix with all non-unique columns removed.
It first transposes the matrix to work with rows instead of columns, making it easier to apply the
duplicated
function. Then, it finds and removes duplicated rows in the transposed matrix, which correspond
to non-unique columns in the original matrix. Finally, it transposes the matrix back to its original
orientation, now with only unique columns remaining.
eliminateNonUniqueColumns(matrix)
A matrix with non-unique columns removed, maintaining the original order of the remaining columns.
exampleMatrix <- matrix(c(1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5, 6), nrow = 3, byrow = TRUE)
print(exampleMatrix)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 2 3 1
#> [2,] 2 3 1 2
#> [3,] 3 4 5 6
# [,1] [,2] [,3] [,4]
# [1,] 1 2 3 1
# [2,] 2 3 1 2
# [3,] 3 1 2 4
uniqueMatrix <- eliminateNonUniqueColumns(exampleMatrix)
print(uniqueMatrix)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 2 3 1
#> [2,] 2 3 1 2
#> [3,] 3 4 5 6
# [,1] [,2]
# [1,] 1 1
# [2,] 2 2
# [3,] 3 4