“Why am I getting a subscript out of bounds error in R when using the Fix function?”

Why am I getting a subscript out of bounds error in R when using the Fix function?

A subscript out of bounds error in R occurs when attempting to access an element or value in a data structure that does not exist. This error can occur when using the Fix function, which is used to modify and replace values in a data set. The error may arise if the specified index is larger than the size of the data structure or if the data structure is not properly defined. It is important to carefully check the syntax and parameters of the Fix function to avoid this error.

Fix in R: Subscript out of bounds


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

Cite this article

stats writer (2024). Why am I getting a subscript out of bounds error in R when using the Fix function?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/why-am-i-getting-a-subscript-out-of-bounds-error-in-r-when-using-the-fix-function/

stats writer. "Why am I getting a subscript out of bounds error in R when using the Fix function?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/why-am-i-getting-a-subscript-out-of-bounds-error-in-r-when-using-the-fix-function/.

stats writer. "Why am I getting a subscript out of bounds error in R when using the Fix function?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/why-am-i-getting-a-subscript-out-of-bounds-error-in-r-when-using-the-fix-function/.

stats writer (2024) 'Why am I getting a subscript out of bounds error in R when using the Fix function?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/why-am-i-getting-a-subscript-out-of-bounds-error-in-r-when-using-the-fix-function/.

[1] stats writer, "Why am I getting a subscript out of bounds error in R when using the Fix function?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. Why am I getting a subscript out of bounds error in R when using the Fix function?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top