You are currently viewing How to Subset a Matrix in R?

In R, subsetting matrices involves extracting specific elements, rows, or columns based on certain conditions or indices. Implement this subsetting using bracket notation([]) and the subset() function of base R. You can use the subset() function to subset the dataframe and subset the vector by name and index.

In this article, I will explain the concept of subsetting and using various approaches of R how we can implement the subsetting of the matrices by rows and columns based on indexes, names, and conditions.

What is R Matrix?

In R, a matrix is a two-dimensional data structure that contains elements of the same data type arranged in rows and columns. It is an extension of a vector, which is a one-dimensional structure. A Matrix is a collection of two or more R Vectors. R Data Frame is another data structure with two dimensional to store elements of different data types.

Subset an R Matrix by a Specific Element

To subset specific elements from the R matrix you can use bracket notation [], by using this notation we can subset a single element from a matrix, multiple elements, subset by range, select elements from a list etc.

Let’s create a matrix from vectors by specifying the number of columns and number of rows.


# Create R matrix
data <- c(10,11,12,13,14,15,16,17,18)
my_matrix <- matrix(data,nrow=3,ncol=3,byrow=TRUE)
my_matrix

Yields below output.

r subset matrix

To subset a specific element of a given matrix you can pass the specified row index and column index(which is the location of a specific element you want) into bracket notation []. It will return the subset of the given Matrix by a specific element.


# Subset a matrix by a specific element 
element <- my_matrix[2, 3]  
print("Subset a matrix by a specific element:")
print(element)

Yields below output.

r subset matrix

Subset a R Matrix by a Specific Row

To subset a matrix by a specific row, you can use bracket notation([]). To do this, simply specify the row index on the left side of the notation(before a comma), and the matrix will be subsetted by the corresponding row of the specified index.


# Subset a matrix by a specific row
row <- my_matrix[2, ]  
print("Subset a matrix by a specific row:")
print(row)

# Output:
# Subset a matrix by a specific row:
# [1] 11 14 17

Subset a Matrix by a Specific Column

Alternatively, you can subset a matrix by a specific column using bracket notation([]). This time you can simply specify the column index on the right side of the notation(after a comma), and the matrix will be subsetted by the corresponding column of the specified index.


# Subset a matrix by a specific column
col <- my_matrix[, 3]  
print("Subset a matrix by a specific column:")
print(col)

# Output:
# Subset a matrix by a specific column:
# [1] 16 17 18

Subset a R Matrix by Logical Condition

Subsetting a matrix by rows based on logical conditions is possible. For instance, you can select rows that meet a specific condition and those rows will be included in the result.


rows <- my_matrix[my_matrix[, 1] > 11,]
print("Subset a matrix by a row based on condition:")
print(rows)

# Output:
# Subset a matrix by a row based on condition:
#      [,1] [,2] [,3]
# [1,]   13   14   15
# [2,]   16   17   18

The resulting matrix will contain all columns of the original matrix, but only rows 2 and 3 are selected where the values in the first column for those rows are greater than 11.

Subsetting by Row and Column Indexes

To subset a matrix by both rows and columns of the matrix. For that, you can simply select rows and columns indexes using vectors. It will subsetting the given matrix by the intersection of rows and columns of specified corresponding indexes.


# Subset a matrix by both column and row indexes
subset <- my_matrix[c(1, 3), c(2, 3)] 
print("Subset a matrix by both column and row indexes:")
print(subset)

# Output:
# Subset a matrix by both column and row indexes:
#      [,1] [,2]
# [1,]   11   12
# [2,]   17   18

So, subset includes the values at the intersection of rows 1 and 3 with columns 2 and 3 from my_matrix.

Using subset() function Subset the R Matrix

So far, we have implemented subsetting of matrices using bracket notation([]). Now we will implement subsetting the matrix by using the subset() function that provides a concise way to filter data frames or matrices based on conditions. In this example, I will subset the matrix by rows based on the condition using this function.


# Subsetting a matrix by rows using subset()
rows <- subset(my_matrix, my_matrix[, 1] > 11)
print("Subsetting by rows using subset():")
print(rows)

# Output:
# Subsetting by rows using subset():
#      [,1] [,2] [,3]
# [1,]   13   14   15
# [2,]   16   17   18

Subsetting R Matrix by Columns using subset()

Alternatively, you can subset the R matrix by columns using the subset() function.


# Subsetting by columns using subset()
cols <- subset(my_matrix, select = c(1, 3), subset = my_matrix[, 1] > 10)
print("Subsetting by rows using subset():")
print(cols)

# Output:
# Subsetting by rows using subset():
#      [,1] [,2]
# [1,]   13   15
# [2,]   16   18

Subset a Matrix by Name

To subset a matrix by name in R, you can use the row and column names along with square brackets. Let’s create a matrix with customized column names and row names and use these names to subset a matrix by names.


# Subset a matrix ny a name
# Create a matrix
data <- c(10, 11, 12, 13, 14, 15, 16, 17, 18)
my_matrix <- matrix(data, nrow = 3, ncol = 3, byrow = TRUE)

# Assign row and column names
rownames(my_matrix) <- c("row1", "row2", "row3")
colnames(my_matrix) <- c("col1", "col2", "col3")
my_matrix

# Subset the matrix by row and column names
subset_matrix <- my_matrix[c("row1", "row3"), c("col2", "col3")]
subset_matrix

# Output:
#      col2 col3
# row1   11   12
# row3   17   18    

In this example, the row names are set to row1, row2, and row3, and the column names are set to col1, col2, and col3. The subset_matrix is created by selecting specific rows (row1 and row3) and columns (col2 and col3) from the original my_matrix.

Conclusion

In this article, I have explained the concept of subsetting and using various approaches of R how we can implement the subsetting of the matrices by rows and columns based on index, name, and condition with multiple examples.

Related Articles

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium