What happens when you mutiply 2 vectors of unequal length in R?

This recipe explains what happens when you mutiply 2 vectors of unequal length in R

Recipe Objective**

What happens when you multiply 2 vectors of unequal length in R? When two vectors of unequal length are multiplied, the vector with shorter length will be recycled in such a way that it will match the length of the longer vector and then perform the multiplication operation. This recycling of the shorter vector is known as the recycling rule. This recipe performs multiplication of unequal vector lengths.

Step 1- Define 2 vectors

Two vectors (for e.g : numeric) of unequal lengths are defined , here the length of vector b is a multiple of length of vector b

a <- c(1:10) b <- c(1:5) print(length(a)) print(length(b)) print(a) print(b)

The shorter vector b gets recycled and forms a vector of length (1:10) in order to match the length of vector a..

Step 2 - Multiple the two vectors

print(a*b)

Two vectors (for e.g - numeric) of unequal lengths are defined, here the length of vector b is not a multiple of the length of vector b.

x <- c(1:10) y <- c(1:4) print(length(x)) print(length(y)) print(x) print(y)
"Output of the code is:"
[1]  1  2  3  4  5  6  7  8  9 10
[1] 1 2 3 4  

A warning message is displayed when the length of the two vectors are not in proportion —

print(x*y)
"Output of the code is"
Warning message in x * y:
“longer object length is not a multiple of shorter object length”
 [1]  1  4  9 16  5 12 21 32  9 20 
 

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.