Open In App

How to Fix: Subscript out of bounds in R

Last Updated : 18 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Subscript out of bounds: It is one of the most common errors that one may encounter in R and has the following form:

Error in y[,6]: subscript out of bounds

Reason: The compiler produces this error when a programmer tries to access a row or column that doesn’t exist.  

Creating a matrix:

Let us firstly create a matrix. For example, we have created a matrix mat having 5 rows and 3 columns. Its values are initialized using sample.int() function. This function is used for taking random elements from a dataset. 

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
#print matrix
print(mat)


Output:

Output

Example 1: Subscript out of bounds (in rows):

The below code is trying to access the 6th row that doesn’t exist.

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Try to print the 6th row of matrix
mat[6, ]


Output:

Output

The 6th row of the matrix does not exist therefore we are getting the subscript out of bounds error. Note that we can always use nrow() function to check how many rows are there in the matrix:

Example:

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Print the number of rows in matrix
nrow(mat)


Output:

Output

There are only 5 rows in the matrix. Hence, we can access the column less than or equal to 5. Now let’s try to access the 3rd column.

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Try to print the 5th row of the matrix
mat[5,]


Output:

Output

Example 2: Subscript out of bounds (in columns).

The below code is trying to access the 4th column that doesn’t exist.

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Try to print the 4th column of the matrix
mat[, 4]


Output:

Output

The 4th column of the matrix does not exist therefore we are getting the subscript out of bounds error. Note that we can always use ncol() function to check how many columns are there in the matrix. 

Example:

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Print the number of columns in the matrix
ncol(mat)


Output:

Output

There are only 3 columns in the matrix. Hence, we can access the column less than or equal to 3. Now let’s try to access the 3rd column.

Example:

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Try to print the 3th column of the matrix
mat[, 3]


Output:

Output

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

The below code is trying to access the 6th row and the 4th column.

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Try to print the 6th row and the 4th column
# of the matrix
mat[6, 4]


Output:

Output

The 6th row and the 4th column of the matrix do not exist therefore we are getting the subscript out of bounds error. Note that we can always use dim() function to check how many rows and columns are there in the matrix.

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Print the number of rows and columns in the matrix
dim(mat)


Output:

Output

There are only 5 rows and 3 columns in the matrix. Hence, we can access the row less than or equal to 5 and the column less than or equal to 3. Now let’s try to access the value stored at the 5th row and the 3rd column.

R




# Create a matrix with 5 rows and 3 columns
mat = matrix(data = sample.int(100, 30), nrow = 5, ncol = 3)
  
# Print the number of rows and columns 
# in the matrix
mat[5,3]


Output:

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads