mistaken collection of subscripts on matrix


One error you could come across in R is:

Error in x[i, ] <- 0 : mistaken collection of subscripts on matrix

This mistake happens whilst you struggle to assign some price to a place in a vector, however accidently come with a comma as should you have been assigning some price to a row and column place in a matrix.

This educational stocks precisely the best way to medication this mistake.

Instance 1: Healing Error for a Unmarried Worth

Assume we’ve please see vector in R with 5 values:

#outline vector
x <- c(4, 6, 7, 7, 15)

Now think we struggle to assign the worth ’22’ to the 3rd part within the vector:

#struggle to assign the worth '22' to part in 3rd place
x[3, ] <- 22

Error in x[3, ] <- 22 : mistaken collection of subscripts on matrix

We obtain an error as a result of we integrated a comma when making an attempt to assign the untouched price.

In lieu, we merely wish to take away the comma:

assign the worth '22' to part in 3rd place
x[3] <- 22

#show up to date vector
x

[1]  4  6 22  7 15

Instance 2: Healing Error in a for Loop

This mistake too can happen once we struggle to exchange a number of values in a vector the use of a β€˜for’ loop.

For instance, please see code makes an attempt to exchange each price in a vector with a 0:

#outline vector
x <- c(4, 6, 7, 7, 15)

#struggle to exchange each price in vector with 0
for(i in 1:field(x)) {
    x[i, ]=0
  }

Error in x[i, ] = 0 : mistaken collection of subscripts on matrix

We obtain an error as a result of we integrated a comma when making an attempt to assign the zeros.

In lieu, we merely wish to take away the comma:

#outline vector
x <- c(4, 6, 7, 7, 15)

#substitute each price in vector with 0
for(i in 1:field(x)) {
    x[i]=0
  }

#view up to date vector
x

[1] 0 0 0 0 0

When we take away the comma, the code runs with out mistakes.

Alternative Assets

Healing in R: NAs Presented via Coercion
Healing in R: Subscript out of bounds
Healing Error in R: mistaken collection of dimensions

Leave a Comment