HomeCommon R Error MessagesFixing 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.

You are watching: R longer object length is not a multiple of shorter object length

## 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 + yWarning message:In x + y : longer object length is not a multiple of shorter object lengthIn 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?

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.**

See more: Eazy-E Only If You Want It, Only If You Want It (Official Video)

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 + yIn 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 + yIn 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.**

See more: Final Fantasy 14 The Greatest Story Never Told, Eorzea Database: The Greatest Story Never Told

> 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 + yHere 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!**How To Make a Frequency Table in RHow To Make a Side-By-Side Boxplot in RR Error in eval(predvarsHow To Use colMeans in RCommon R Error Messages

report this ad

Resources to help you simplify data collection and analysis using R. Automate all the things!

**Web Scraping with R (Examples)****Monte Carlo Simulation in R****Connecting R to Databases****Animation & Graphics****Manipulating Data Frames****Matrix Algebra Operations****Sampling****Statistics****Common Errors**

report this ad

## Categories

ArticlesBook reviewsFeatured R consultantsHelpPython HelpR consultantsR job listingsR jobs listservSite news