How to Solve R Warning: longer object length is not a multiple of shorter object length

by | Programming, R, Tips

In R, if you perform an operation on objects with different lengths, you will get the warning message: longer object length is not a multiple of shorter object length.

This warning happens specifically if the length of the longer object is not a multiple of the shorter object

In the case of vectors, you can append zeros to the end of the shorter vector using a for loop, for example:

for(i in ((length(y)+1):length(x)))
    {y = c(y,0)}

This tutorial will go through the warning in detail and how to solve it with code examples.


Example #1

Consider two vectors, which we want to add together:

x <- c(1, 3, 4, 9, 10)
y <- c(2, 4, 5)

x + y

When we run the code we get a warning message:

[1]  3  7  9 11 14
Warning message:
In x + y : longer object length is not a multiple of shorter object length

If the R objects are of different lengths, R will still perform the operation but will replicate the elements from the shorter vector to continue.

The last two elements of the resultant vector is 11, which is the fourth element of x (9) plus the first element of y (2), and 14, which is the fifth element of x (10) plus the second element of y (4). R will auto-replicate objects so that they are the same length if they differ. However, if the length of the shorter vector is not a factor of the length of the longer vector, R will raise the warning message.

It is a warning message instead of an error because R can still perform the operation, but the data may be mismatched.

We can verify the vectors are of different lengths by using the length() function:

length(x)
length(y)
[1] 5
[1] 3

Solution

We can solve this error by making sure that the vectors have the same length. We can use a for loop to append zeroes to the shorter vector. Let’s look at the revised code:

x <- c(1, 3, 4, 9, 10)
y <- c(2, 4, 5)

for(i in ((length(y)+1):length(x)))
    {y = c(y,0)}

x
y

length(x)

length(y)
[1]  1  3  4  9 10

[1] 2 4 5 0 0 

[1] 5

[1] 5

The vectors are the same length. Let’s try to perform the addition operation again:

x + y
[1]  3  7  9  9 10

This time we do not get a warning because the vectors are of equal length.

Example #2

Let’s look at a second example where the shorter vector is only short by one. W

x <- c(1, 3, 4, 9, 10)
y <- c(2, 4, 5, 7)

x + y

If we run the code, we will get the same warning message:

[1]  3  7  9 16 12
Warning message:
In x + y : longer object length is not a multiple of shorter object length

We can verify the lengths of the vectors using the length() function.

length(x)
length(y)
[1] 5
[1] 4

Solution

As the vector y is shorter than x by one, we can append one zero to the end as follows:

x <- c(1, 3, 4, 9, 10)
y <- c(2, 4, 5, 7)

y <- c(y, 0)

x + y

Let’s run the code to see what happens:

[1]  3  7  9 16 10

Example #3

Note that if the length of the shorter vector is a factor of length of the longer vector, we will not get the error message. Let’s look at an example:

x <- c(3, 6, 9, 12, 15, 18)
y <- c(2, 4)

length(x)
length(y)
[1] 6
[1] 2

The length of y is 2 and the length of x is 6, of which 2 is a factor. Let’s try to add the two vectors together:

x + y
[1]  5 10 11 16 17 22

Note that R does not raise the warning message and auto-replicates the shorter vector y to complete the addition operation.

Solution

If you do not want R to auto-replicate the shorter vector you can add an if-statement to only perform the operation if the vectors are equal in length.

x <- c(3, 6, 9, 12, 15, 18)
y <- c(2, 4)

if(length(x)==length(y)){
x + y} else {
print('vectors are not of equal length:')
}

Let’s run the code to see the result:

[1] "vectors are not of equal length"

With the if-statement we can control when to perform the addition operation.

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!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨