Why this error might occur?
This type of error occurs when the programmer tries to apply matrix notation to a vector.
Example:
In the below source code, we have created a vector vect (1 – dimensional data structure). Using a for-loop we are iterating from i = 1 to i = 100 and at each step of the iteration, we are assigning the value 10 to all columns of the ith row. But vect is a 1-dimensional data structure and we are applying matrix notation on it. Hence, the below source code will lead to an error: “the incorrect number of subscripts on the matrix” as clearly seen in the output.
R
# R program to illustrate reasons for the error # Creating a vector vect = rep (1, 5) # Assign values in the created vector for (i in 1:5) { # Assigning at all columns of the ith row vect[i,] = 10 } |
How to Fix: incorrect number of subscripts on matrix in R
Getting errors in programming is quite common and their frequency increase when our source code becomes bulkier. In this article, we are going to discuss how to fix: the incorrect number of subscripts on the matrix in R.
This error doesn’t give us any clue at the first appearance. This error may occur due to a very small mistake. This error may be easy for programmers to make but finding this error sometimes becomes challenging for most of the programmers.