Warning Message in R: longer object length not multiple of shorter object
In this article you’ll learn how to deal with the warning message “longer object length is not a multiple of shorter object length” in R programming.
The post looks as follows:
So now the part you have been waiting for – the exemplifying R code.
Example 1: Reproduce the Warning – longer object length is not a multiple of shorter object length
In this example, I’ll explain how to replicate the warning message “longer object length is not a multiple of shorter object length” in R. Consider the following R syntax:
1:5 + 1:6 # Using two vectors with different length # [1] 2 4 6 8 10 7 # Warning message: # In 1:5 + 1:6 : # longer object length is not a multiple of shorter object length |
1:5 + 1:6 # Using two vectors with different length # [1] 2 4 6 8 10 7 # Warning message: # In 1:5 + 1:6 : # longer object length is not a multiple of shorter object length
As you can see, the RStudio console has returned the warning message “longer object length is not a multiple of shorter object length”.
The reason for this is that we have tried to add two vector objects with different length (i.e. a vector containing five elements and a vector containing six elements).
The R programming language therefore had to cycle through the vector with shorter length and use the first element twice, i.e. the last element 6 of the second vector was added to the first element 1 of the first vector.
So how can we solve this problem?
Example 2: Fix the Warning Message – longer object length is not a multiple of shorter object length
In this example, I’ll illustrate how to avoid the warning message “longer object length is not a multiple of shorter object length”. For this, we simply have to use two data objects with the same length:
1:5 + 1:5 # Using two vectors with same length # [1] 2 4 6 8 10 |
1:5 + 1:5 # Using two vectors with same length # [1] 2 4 6 8 10
Looks good!
Video, Further Resources & Summary
Would you like to know more about warnings in R? Then you may have a look at the following video of my YouTube channel. I illustrate the R programming codes of this tutorial in the video.
The YouTube video will be added soon.
In addition, I can recommend having a look at the related tutorials on my website:
- Warning Message: Condition Length > 1 Only First Element Will Be Used
- Error: Object of Type Closure is not Subsettable in R
- Error: Object X not Found in R
- Fixing Errors & Warnings in R (Cheat Sheet)
- Introduction to R
Summary: At this point you should have learned how to handle the warning message “longer object length is not a multiple of shorter object length” in R programming. In case you have any further questions and/or comments, let me know in the comments.