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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.