How to fix in R: Subscript out of bounds

Subscript out of bounds is an error that occurs when trying to access an element at an index that is outside the range of the vector or matrix in R. It can be fixed by checking the data structure and indexing, and making sure that the index used to access the element is within the range of the vector or matrix. If the index is too large, you can reduce it to the maximum index size in the vector or matrix. If the index is too small, you can increase it to the minimum index size in the vector or matrix.


One common error you may encounter in R is:

Error in x[, 4] : subscript out of bounds

This error occurs when you attempt to access a column or row in a matrix that does not exist.

This tutorial shares the exact steps you can use to troubleshoot this error, using the following matrix as an example:

#make this example reproducible
set.seed(0)

#create matrix with 10 rows and 3 columns
x = matrix(data = sample.int(100, 30), nrow = 10, ncol = 3)

#print matrix
print(x)

      [,1] [,2] [,3]
 [1,]   14   51   96
 [2,]   68   85   44
 [3,]   39   21   33
 [4,]    1   54   35
 [5,]   34   74   70
 [6,]   87    7   86
 [7,]   43   73   42
 [8,]  100   79   38
 [9,]   82   37   20
[10,]   59   92   28

Example #1: Subscript out of bounds (with rows)

The following code attempts to access the 11th row of the matrix, which does not exist:

#attempt to display 11th row of matrix
x[11, ]

Error in x[11, ] : subscript out of bounds

Since the 11th row of the matrix does not exist, we get the subscript out of bounds error.

If we’re unaware of how many rows are in the matrix, we can use the nrow() function to find out:

#display number of rows in matrix
nrow(x)

[1] 10

We can see that there are only 10 rows in the matrix. Thus, we can only use numbers less than or equal to 10 when accessing the rows.

For example, we can use the following syntax to display the 10th row of the matrix:

#display 10th row of matrix
x[10, ]

[1] 59 92 28

Example #2: Subscript out of bounds (with columns)

The following code attempts to access the 4th column of the matrix, which does not exist:

#attempt to display 4th column of matrix
x[, 4]

Error in x[, 4] : subscript out of bounds

If we’re unaware of how many columns are in the matrix, we can use the ncol() function to find out:

#display number of columns in matrix
ncol(x)

[1] 3

We can see that there are only 3 columns in the matrix. Thus, we can only use numbers less than or equal to 3 when accessing the columns.

For example, we can use the following syntax to display the 3rd column of the matrix:

#display 3rd column of matrix
x[, 3]

[1] 96 44 33 35 70 86 42 38 20 28

Example #3: Subscript out of bounds (rows & columns)

The following code attempts to access the value in the 11th row and the 4th column of the matrix, which does not exist:

#attempt to display value in 11th row and 4th column
x[11, 4]

Error in x[11, 4] : subscript out of bounds

Since neither the 11th row nor the 4th column of the matrix exist, we get the subscript out of bounds error.

If we’re unaware of how many rows and columns are in the matrix, we can use the dim() function to find out:

#display number of rows and columns in matrix
dim(x)

[1] 10  3

We can see that there are only 10 rows and 3 columns in the matrix. Thus, we can only use numbers less than or equal to these values when accessing the rows and columns.

For example, we can use the following syntax to display the value in the 10th row and the 3rd column of the matrix:

#display value in 10th row and 3rd column of matrix
x[10, 3]

[1] 28

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: longer object length is not a multiple of shorter object length

x