Reproducing the error
Let us consider an example in which we have a vector initialized with 5 values in it.
R
# Initialize a vector myVector <- c (10,13,2,6,12) |
As you can see in the snippet that myVector is a one-dimensional vector having 5 values in it. Now suppose we mistakenly try to subset it with two dimensions:
Example:
In this program, we are accessing the value stored at the 4th row and the 1st column.
R
# Initialize a vector myVector <- c (10, 13, 2, 6, 12) # Access the value stored at 4th row # and 1st column myVector[4, ] |
Output:
Example:
In this program, we are accessing the value stored at the 1st row and the 4th column. And here the R compiler produces this error because we are trying to subset with 2-dimensions while the vector has 1-dimension.
R
# Initialize a vector myVector <- c (10, 13, 2, 6, 12) # Access the value stored at 4th # row and 1st column myVector[, 4] |
Output:
How to Fix in R: incorrect number of dimensions
In this article, we will discuss how we can fix the “incorrect number of dimensions” error in R Programming Language.
One common error that one may face in R is:
Error in [x, 10] : incorrect number of dimensions
The R compiler produces such an error when one tries to refer to an object by providing more dimensions than the actual dimension the object has.