Extract Values from Matrix by Column & Row Names in R (3 Examples)

 

In this article, I’ll show how to get certain column and row values using column and row names of a matrix in the R programming language.

The content of the article looks as follows:

So now the part you have been waiting for – the examples…

 

Example Data

First, we’ll have to create some data that we can use in the examples below.

my_matrix <- matrix(1:15, ncol = 5)                # Create example matrix
colnames(my_matrix) <- paste0("Col", 1:5)
rownames(my_matrix) <- paste0("Row", 1:3)
my_matrix                                          # Print example matrix
#      Col1 Col2 Col3 Col4 Col5
# Row1    1    4    7   10   13
# Row2    2    5    8   11   14
# Row3    3    6    9   12   15

As you can see based on the previously shown RStudio console output, our example matrix has three rows and five columns. The rows of our matrix are named Row1 – Row3 and the variables are named Col1 – Col5.

Let’s extract some values of our matrix!

 

Example 1: Extracting Certain Columns of Matrix by Column Names

In Example 1, I’ll illustrate how to use the variable names of our matrix to get the values of certain columns.

Consider the following R code:

my_matrix_col <- my_matrix[ , c("Col2", "Col5")]   # Extract columns
my_matrix_col                                      # Print updated matrix
#      Col2 Col5
# Row1    4   13
# Row2    5   14
# Row3    6   15

Have a look at the previous output of the RStudio console: We have extracted all row values of the columns Col2 and Col5.

 

Example 2: Extracting Certain Rows of Matrix by Row Names

In Example 2, I’ll show how to extract certain rows by using the row names of our matrix:

my_matrix_row <- my_matrix[c("Row2", "Row3"), ]    # Extract rows
my_matrix_row                                      # Print updated matrix
#      Col1 Col2 Col3 Col4 Col5
# Row2    2    5    8   11   14
# Row3    3    6    9   12   15

We have extracted all columns, but only the second and third row were kept.

 

Example 3: Extracting Certain Columns & Rows of Matrix by Column & Row Names

Example 3 explains how to use column and row names simultaneously to extract specific data points from our matrix:

my_matrix_col_row <- my_matrix[c("Row1", "Row3"),  # Extract columns & rows
                               c("Col1", "Col4", "Col5")]
my_matrix_col_row                                  # Print updated matrix
#      Col1 Col4 Col5
# Row1    1   10   13
# Row3    3   12   15

Looks good!

 

Video, Further Resources & Summary

Do you need further information on the R programming codes of the present article? Then you could watch the following video of my YouTube channel. I’m showing the R codes of this post in the video.

 

The YouTube video will be added soon.

 

In addition, you could have a look at the other articles of this website:

 

In this tutorial, I explained how to extract particular values of a matrix in the R programming language. Let me know in the comments, in case you have any additional questions.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top