# 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.

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 the Statistics Globe Newsletter