Error in X : incorrect number of subscripts on matrix in R (2 Examples)

In this article, I’ll show how to debug the error “incorrect number of subscripts on matrix” in the R programming language.

Creation of Example Data

my_vec <- LETTERS[1:10]        # Example vector in R
my_vec                         # Display example vector in RStudio
#  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"

Example 1: Replicating the Error Message – incorrect number of subscripts on matrix

my_vec[6, 7] <- "XXX"          # Using two dimensions for one-dimensional vector
# Error in x[1, 3] <- 7 : incorrect number of subscripts on matrix

Example 2: Solving the Error Message – incorrect number of subscripts on matrix

my_vec[6] <- "XXX"             # Correct number of dimensions
my_vec                         # Display vector with replaced value
#  [1] "A" "B" "C" "D" "E" "XXX" "G" "H" "I" "J"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top