Get Started →
Subscript & Indexing Errors

Subscript out of bounds

Matches: subscript out of bounds

WHAT THIS ERROR MEANS

You tried to access an element at a position that doesn't exist — like asking for the 10th element of a 5-element vector.

HOW TO FIX IT

1. Check the length of your object with length() or nrow()/ncol().

2. Make sure your index is within the valid range.

3. Remember R is 1-indexed (first element is [1], not [0]).

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
x <- c(1, 2, 3)
x[5]  # only 3 elements
GOOD — CORRECT APPROACH
x <- c(1, 2, 3)
x[3]  # valid index

Still stuck?

Paste your code in RChat and the AI will fix this error in context.

Try RChat Free →

RELATED SUBSCRIPT & INDEXING ERRORS