Select Page

# How to Solve R Error: Incorrect number of subscripts on matrix

by | Programming, R, Tips

This error occurs when you try to assign a value to a position in a vector but have a comma next to the position. R interprets the comma as trying to assign a value to a row or column position in a matrix. You can solve this error by removing the comma. For example,

```x <- c(2, 4, 6, 8, 10, 12, 14)

x[1] <- 20```

This tutorial will go through the error in detail with code examples.

## Example #1

Let’s look at an example to reproduce the error. First, we will define a vector with seven values:

`x <- c(2, 4, 6, 8, 10, 12, 14)`

Next, we will try to assign the value `76` to the fourth element in the vector:

`x[4, ] <- 76`

Let’s run the code to see what happens:

`Error in x[4, ] <- 22 : incorrect number of subscripts on matrix`

The error occurs because we put a comma after the position. We can use the comma syntax to handle 2-dimensional data structures like a matrix. For example,

```mat = matrix(rep(0,5), nrow=5, ncol=5)
mat```
```     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0```

The syntax `mat[i,]` returns the ith row of the matrix and `mat[,i]` returns the ith column of the matrix.

`x`
` [1] 0 0 0 0 0 0 0 0 0 0`

The vector `x` is one-dimensional, and therefore, the comma syntax is not valid.

### Solution

We can solve the error by removing the comma. Let’s look at the revised code.

```x <- c(2, 4, 6, 8, 10, 12, 14)

x[4] <- 76

x```

Let’s run the code to get the result:

```[1]  2  4  6 76 10 12 14
```

We successfully assigned the value `76` to the fourth value in the matrix.

## Example #2

Let’s look at a second example where we try to replace the values in a vector using a `for` loop.

```# Define vector

x <- rep(1,10)

# Attempt to replace values in vector with zero

for (i in 1:length(x)) {

x[i,] = 0

}```

Let’s run the code to see what happens:

`Error in x[i, ] <- 0 : incorrect number of subscripts on matrix`

The error occurs because we included a comma when trying to assign zeros to the vector.

### Solution

We can solve this error by removing the comma

```# Define vector of ones

x <- rep(1,10)

# Attempt to replace values in vector with zeros

for (i in 1:length(x)) {

x[i] = 0

}

x```

Let’s run the code to see the result:

`[1] 0 0 0 0 0 0 0 0 0 0`

We successfully replaced the values in the vector with zeros.

## Assigning Values to a Matrix

Let’s look at an example of assigning values to a matrix using the comma syntax.

```mat = matrix(rep(0,5), nrow=5, ncol=5)

for(i in 1:5)
{
mat[i,] <- 98
}

mat```
```     [,1] [,2] [,3] [,4] [,5]
[1,]   98   98   98   98   98
[2,]   98   98   98   98   98
[3,]   98   98   98   98   98
[4,]   98   98   98   98   98
[5,]   98   98   98   98   98```

## Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles:

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!

##### Suf
Research Scientist at | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!