*62*

One common warning message you may encounter in R is:

Warning message: In a + b : longer object length is not a multiple of shorter object length

This warning message occurs when you attempt to perform some operation across two or more vectors that donâ€™t have the same length.

This tutorial shares the exact steps you can use to troubleshoot this warning message.

**How to Reproduce the Warning Message**

Suppose we add the values of the following two vectors in R:

#define two vectors a #add the two vectors a + b [1] 7 9 11 13 15

The resulting vector shows the sum of the corresponding values in each vector.

We received no warning message because the two vectors are of equal length.

However, suppose the second vector had one less value than the first value:

#define two vectors a #add the two vectors a + b [1] 7 9 11 13 11 Warning message: In a + b : longer object length is not a multiple of shorter object length

Since the two vectors have different lengths, we get the **longer object length is not a multiple of shorter object length** warning message.

Itâ€™s important to note that R still forces the calculation to work by adding the last value of the first vector (5) with the first value of the second vector (6) to come up with the final value of **11**.

If weâ€™re unaware of the length of each vector, we can use the **length()** function to find out:

#display length of vector a length(a) [1] 5 #display length of vector b length(b) [1] 4

We can see that the first vector has 5 values while the second vector only has 4 values. This is why we receive a warning message.

**How to Fix the Warning Message**

To fix this warning message, we simply need to make sure that both vectors have the same length.

For example, if we know that vector b has one less value than vector a, then we can simply add a zero to the end of vector b**:**

#define two vectors a #add zero to the end of vector b b #add the two vectors a + b [1] 7 9 11 13 5

In most cases, we donâ€™t actually know the difference in lengths between the two vectors so we can use the following **for loop** to add the correct amount of zeros to the end of the shorter vector:

#define two vectors a #add zeros to the end of vector b for(i in ((length(b)+1):length(a))) +{b = c(b, 0)} #add the two vectors a + b [1] 7 9 11 13 5

The warning message goes away because we added enough zeros to the end of vector b to ensure that both vectors had the same length.

**Additional Resources**

The following tutorials explain how to troubleshoot other common errors in R:

How to Fix in R: names do not match previous names

How to Fix in R: NAs Introduced by Coercion

How to Fix in R: Subscript out of bounds

How to Fix in R: contrasts can be applied only to factors with 2 or more levels