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.

FastText and Word2Vec Word Embeddings Python Implementation

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

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.