How to Solve R Error: Subscript out of bounds

by | Programming, R, Tips

If you try to access a column or row that does not exist, you will raise the error Subscript out of bounds.

You can use nrows(matrix) to find the number of rows of a matrix, ncol(matrix) to find the number of columns in a matrix and dim(matrix) to find the number of rows and columns in a matrix.

Once you know the number of rows and columns, you can index the matrix within those bounds.

This tutorial will go through the error in detail and how to solve it with code examples.


Example #1: Subscript out of bounds with number of rows

Let’s create a matrix with 8 rows and 5 columns.

set.seed(0)
mat = matrix(data=sample.int(100, 40), nrow= 8, ncol=5)
print(mat)
 [,1] [,2] [,3] [,4] [,5]
[1,]   32   70   23   21   34
[2,]   14   87   29   31   10
[3,]    2   92   91   17    1
[4,]   45   75   95   73   43
[5,]   18   81   28   79   59
[6,]   22   13   85   64   26
[7,]   78   40   33   60   15
[8,]   65   48   97   51   58

Let’s try to access the 9th row of the matrix:

print(mat[9,])
Error in mat[9, ] : subscript out of bounds

The error occurs because there are only 8 rows in the matrix. We can use nrow() to find out how many rows are in the matrix:

print(nrow(mat))
[1] 8

The nrow() function confirms there are only eight rows. Let’s see what happens when we try to retrieve the 8th row of the matrix:

print(mat[8,])
65 48 97 51 58

We successfully retrieved the 8th row of the matrix.

Example #2: Subscript out of bounds with number of columns

Let’s try to access the 6th column of the matrix:

print(mat[,6])
Error in mat[, 6] : subscript out of bounds

The error occurs because there are only 5 columns in the matrix. We can use ncol() to find out how many columns are in the matrix:

print(ncol(mat))
[1] 5

The ncol() function confirms there are only 5 columns. Let’s see what happens when we try to retrieve the 5th row of the matrix:

print(mat[,5])
[1] 34 10  1 43 59 26 15 58

We successfully retrieved the 5th column of the matrix.

Example #3: Subscript out of bounds with number of rows and columns

Let’s try to access the value at the 9th row of the 6th column of the matrix:

print(mat[9,6])
Error in mat[9, 6] : subscript out of bounds

The error occurs because there are only 9 rows and 5 columns in the matrix. We can use dim() to find out how many rows and columns are in the matrix:

print(dim(mat))
[1] 8 5

The dim() function confirms there are only 8 rows and 5 columns in the matrix. Let’s see what happens when we try to retrieve the value at the matrix’s 5th row and 8th column.

print(mat[8,5])
[1] 58

Summary

Congratulations on reading to the end of this tutorial! The error Subscript out of bounds occurs when you try to access the column or row of a matrix that does not exist in the matrix. You can solve this error by checking the dimensions of the matrix and indexing within those bounds.


For further reading on R related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee