How to Fix: Subscript out of bounds in R
Last Updated :
18 Mar, 2022
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
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
mat = matrix (data = sample.int (100, 30), nrow = 5, ncol = 3)
mat[5,3]
|
Output:
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...