Extract unique columns from a matrix using R
Last Updated :
24 Apr, 2025
A matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. In R programming Language, matrices are two-dimensional, homogeneous data structures. These are some examples of matrices.

To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector. You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix.
Note: By default, matrices are in column-wise order.
Extract unique columns from a matrix using R
First of all let's make a matrix
R
A = matrix(
# Taking sequence of elements
c(1,2,1, 5,3,5, 1,2,1, 1,2,3 ,1,1,1, 1,5,4, 1,1,1),
# No of rows
nrow = 3,
# No of columns
ncol = 7,
#byrow = TRUE
#bycol= TRUE
# By default matrices are in column-wise order
# So this parameter decides how to arrange the matrix
)
rownames(A) = c("a", "b", "c")
# Naming columns
colnames(A) = c("a", "b", "c", "d", "e", "f", "g")
cat("The 3x7 matrix:\n")
print(A)
Output:
The 3x7 matrix:
a b c d e f g
a 1 5 1 1 1 1 1
b 2 3 2 2 1 5 1
c 1 5 1 3 1 4 1
Method 1: Using unique() function
Now we can see that the columns 1-3 and 5-7 are same so we will use unique() function to extract unique columns out of matrix.
Syntax:
unique(x, incomparables, fromLast, nmax, …,MARGIN)
- x: This parameter is a vector or a data frame or an array or NULL.
- incomparables: This parameter is a vector of values that cannot be compared. If its value is FALSE, that means that all values can be compared, and maybe the only value accepted for methods other than the default. It will be coerced internally to the same type as x.
- fromLast: This parameter indicates that if duplication should be considered from the last, i.e., the rightmost of identical elements will be kept. Its value is logical i.e., either true or false.
- nmax: This parameter says the maximum number of unique items expected.
- … : This is the arguments for particular methods.
- MARGIN: This parameter says the array margin to be held fixed.
- Return value: This function returns a vector, data frame, or array without any duplicate elements/rows.
R
A = matrix(c(1,2,1, 5,3,5, 1,2,1, 1,2,3 ,1,1,1, 1,5,4, 1,1,1),nrow = 3,ncol = 7)
rownames(A) = c("a", "b", "c")
# Naming columns
colnames(A) = c("a", "b", "c", "d", "e", "f", "g")
cat("The matrix:\n")
unique(A, MARGIN=2)
Output:
The matrix:
a b d e f
a 1 5 1 1 1
b 2 3 2 1 5
c 1 5 3 1 4
As it can be observed from the output all the next occurance of the column are removed. More detailed description of unique function is as follows.
Some of the other examples of unique() are
R
# R program to show
# unique() function
# Initializing an input vector with some
# duplicate values
V <- c(1, 2, 3, 4, 4, 5, 6, 5, 6, 5)
# Calling the unique() function over the
# vector to remove duplicate values from it
unique(V)
Output:
[1] 1 2 3 4 5 6
Unique elements from the specified matrix
R
A = matrix(c(1,2,3,4, 1,2,3,4, 1,2,4,4, 1,2,4,4),nrow = 4,ncol = 4,byrow = TRUE)
rownames(A) = c("a", "b", "c", "d")
# Naming columns
colnames(A) = c("a", "b", "c", "d")
cat("The 4x4 matrix:\n")
unique(A)
Output:
The 4x4 matrix:
a b c d
a 1 2 3 4
c 1 2 4 4
as the output depicts all the next occurance of duplicate rows are removed.
Unique elements from the specified dataframe
R
# R program to illustrate
# unique() function
# Creating a data frame
my_class <- data.frame(Student = c('Rohit', 'Anjali',
'Rohit', 'Rohan',
'Anjali'),
Age = c(22, 23, 22, 22, 23), Gender = c('Male', 'Female',
'Male', 'Male',
'Female'))
my_class
# Printing new line
writeLines("\n")
# the unique elements only
unique(my_class)
Output:
Student Age Gender
1 Rohit 22 Male
2 Anjali 23 Female
3 Rohit 22 Male
4 Rohan 22 Male
5 Anjali 23 Female
Student Age Gender
1 Rohit 22 Male
2 Anjali 23 Female
4 Rohan 22 Male
Method 2: Using duplicated() function
In R, the duplicated() function is used to recognize duplicated elements in a vector/duplicated rows in a matrix.
- When used with matrices or data frames, duplicated() returns a logical vector depicting whether each row is a duplicate (i.e., whether it has already occurred above in the data structure).
- As shown below in the code we use !duplicated(t(A)) to get a logical vector indicating which columns are unique after transposing the matrix A.
R
A = matrix(
c(1, 2, 1, 5, 3, 5, 1, 2, 1, 1, 2, 3, 1, 1, 1, 1, 5, 4, 1, 1, 1, 1),
nrow = 3,
ncol = 7
)
rownames(A) = c("a", "b", "c")
colnames(A) = c("a", "b", "c", "d", "e", "f", "g")
cat("The 3x7 matrix:\n")
print(A)
# Extract unique columns
unique_columns <- A[, !duplicated(t(A))]
cat("\nUnique columns:\n")
print(unique_columns)
Output:
The 3x7 matrix:
a b c d e f g
a 1 5 1 1 1 1 1
b 2 3 2 2 1 5 1
c 1 5 1 3 1 4 1
Unique columns:
a b d e f
a 1 5 1 1 1
b 2 3 2 1 5
c 1 5 3 1 4
This code uses the t(A) function which transpose the matrix. duplicated() is then applied to recognize duplicate columns, and ! is used to negate the result. At last columns with no duplicates are selected using A[,!duplicated(t(A))]. The result is the stored in the unique_columns variable and printed as output.
Similar Reads
Extract unique rows from a matrix using R
A matrix is a rectangular representation of elements that are put in rows and columns. The rows represent the horizontal data while the columns represent the vertical data in R Programming Language. Matrix in RIn R we can create a matrix using the function called matrix(). We have to pass some argum
5 min read
Sum All Elements in a Matrix using R
Matrices in R Programming Language are the objects in which the elements are arranged in a 2-D rectangular layout. A matrix is a collection of elements of the same data type(numeric, character, or logical) arranged in a fixed number of rows and columns, as we very well know rows are represented hori
4 min read
Summarise multiple columns using dplyr in R
In this article, we will discuss how to summarise multiple columns using dplyr package in R Programming Language, Method 1: Using summarise_all() method The summarise_all method in R is used to affect every column of the data frame. The output data frame returns all the columns of the data frame whe
3 min read
Convert Multiple Columns to Numeric Using dplyr
In data analysis with R Programming Language, it's common to encounter datasets where certain columns must be converted to numeric type for further study or modeling. In this article, we'll explore how to efficiently convert multiple columns to numeric using the dplyr package in R. Identifying Colum
8 min read
Convert a matrix into a lower triangular matrix using R
In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language. What is a matrix?A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are ve
5 min read
How to Extract Characters from a String in R
Strings are one of R's most commonly used data types, and manipulating them is essential in many data analysis and cleaning tasks. Extracting specific characters or substrings from a string is a crucial operation. In this article, weâll explore different methods to extract characters from a string i
4 min read
Unnesting a list of lists in a data frame column in R
Working with data that has lists within columns is frequent when using R programming language. These lists may include various kinds of information, including other lists. But, working with these hierarchical lists can be difficult, especially if we wish to analyze or visualize the data. A list of l
6 min read
Create a matrix from a list of vectors using R
In R, vectors can be integer, double, character, logical, complex and raw types. A vector is created using the c() function:a <- c(val1, val2, ...Creating a List of VectorsA list of vectors in R is an array of more than one vector kept in a list structure. Creating a list of vectors is done in th
2 min read
Extract Values Above Main Diagonal of a Matrix in R
In this article, we are going to know how to extract the values above the main diagonal of a matrix in the R programming language. The main diagonal of a matrix is a straight path that connects the entries (or elements) in a matrix whose row and column are the same. The number of elements in the mai
3 min read
Extract data.table Column as Vector Using Index Position in R
The column at a specified index can be extracted using the list sub-setting, i.e. [[, operator. The double bracket operator is faster in comparison to the single bracket, and can be used to extract the element or factor level at the specified index. In case, an index more than the number of rows is
2 min read