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

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

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.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

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.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

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.

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.