What happens when you divide 2 vectors of unequal length in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

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

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

Recipe Objective

What happens when you divide 2 vectors of unequal length in R? When two vectors of unequal length are divided, 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 division operation. This recycling of the shorter vector is known as the recycling rule. This recipe performs division of unequal vectors length.

Step 1- Define 2 vectors

Two vectors (for e.g: numeric) of unequal lengths are defined, here the length of vector b is a divide of the 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 - Divide the two vectors

print(a/b)
"Output of the code is"
 [1] 1.000000 1.000000 1.000000 1.000000 1.000000 6.000000 3.500000 2.666667
 [9] 2.250000 2.000000 

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)

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.000000 1.000000 1.000000 1.000000 5.000000 3.000000 2.333333 2.000000
 [9] 9.000000 5.000000

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.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

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.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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 Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

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.