Fixing R Error Message: longer object length is not a multiple of shorter object length

As annoying as error messages may be, they tell you that something is going wrong. Often the message itself is not particularly helpful, but there are cases where it tells you exactly what the problem is. The “longer object length is not a multiple of shorter object length” message is an example one that tells you what the actual problem is.

When Does This Error Occur?

The “longer object length is not a multiple of shorter object length” R error occurs when you are performing a function on vectors, and the vectors are not of the same length. In such cases, the smaller vector restarts from the beginning.

> x = c(1,2,3,4,5,6,7)
> y = c(1,2,3,4,5,6,7,8)
> z = x + y
Warning message:
In x + y : longer object length is not a multiple of shorter object length

In this example “x” has seven numbers and “y” has eight. Consequently “X” the formula comes to the end of “x” it has to start over. If the length of “y” is not a multiple of the length of “x” the process will not return to the end of “x.”

What is causing this error?

in is na e1 is na e2 : longer object length is not a multiple of shorter object length longer object length is not a multiple of shorter object length str_detect shorter longer object length is not a multiple of shorter object length ifelse longer object length is not a multiple of shorter object length rmse in b * sx : longer object length is not a multiple of shorter object length in txid == raw((txidcol longer object length is not a multiple of shorter object length)) multiple cv.glm longer object length is not a multiple of shorter object length not in r

The longer object length is not a multiple of shorter object length R warning message appears when you place vectors of different lengths through a function. It occurs because the smaller vector does not end when the larger one does.

> x = c(1,2,3,4,5,6,7,8)
> y = c(1,2,3,4,5,6,7,8)
> z = x + y

In this first example, “x” and “y” have the same length and so no message is produced.

> x = c(1,2,3,4)
> y = c(1,2,3,4,5,6,7,8)
> z = x + y

In this example, “x” has a length that is half the length of “y” and no message is produced. This is because in this situation “x” and “y” end at the same time. What triggers this message is that the vectors cannot end together. It is caused by a smaller one not being at its end when the largest one reaches its end.

How Do I Fix This Error?

Fixing this problem is quite easy! All you need to do is make sure that the vectors are the same length. When you only have two vectors, it is also easy to make sure the larger length is a multiple of the smaller. This solution becomes more difficult when you use more than two vectors.

> x = c(1,2,3,4,5,6,7)
> y = c(1,2,3,4,5,6,7,8)
> x = c(x, 0)
> z = x + y

In this simple case, all that is needed to fix this error is to add a zero to the end of “x” but this approach only works when you know the lengths.

> x = c(1,2,3,4,5)
> y = c(1,2,3,4,5,6,7,8)
> for(i in ((length(x)+1):length(y)))
+ { x = c(x, 0)}
> z = x + y

Here is a more general way to fix this problem. It dynamically adds zeros to the end of “x” depending on the relative lengths of “x” and “y.” This method can be used with any adjustment you want to use to make vectors the same length.

We hope this tutorial on fixing the “longer object length is not a multiple of shorter object length” R Error was helpful, and encourage you to check out the rest of our site for all of your R programming needs!