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

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

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


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 —

"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 

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.