What happens when you apply relational operations on 2 vectors of equal and unequal length in R?

This recipe explains what happens when you apply relational operations on 2 vectors of equal and unequal length in R

Recipe Objective

Vector is a type of object or data structure in R-language. They are designed to store multiple values of same data-type. For example: if you want to store different 50 food items for each cuisine, you don't need to create 50 variables for each cuisine but just a vector of length 50 of datatype character.

Note: It can not have a combination of any two datatype. It has to be homogeneous in nature.

Relational operations between two vector is where every element of vector is compared to the corresponding element in another vector. We use different Relational operators to carry out this task. The output of the operation is a boolean vector.

Access Text Classification using Naive Bayes Python Code

This recipe demonstrates relational operation with two examples:

  1. comparing 2 vectors with equal length
  2. comparing 2 vectors with unequal length

1. comparing 2 vectors with equal length

Step 1: Creating 2 vectors

We use combine function "c()" to create a vector

a = c(2,5,8,9,10) b = c(5,6,10,20,2)

Step 2: Element-wise comparison

 

We use relational operator greater than (">") to illustrate this operation. This gives us a boolean vector specifying whether the condition was met or not

print(a>b)

[1] FALSE FALSE FALSE FALSE  TRUE

 

2. comparing 2 vectors with unequal length

 

Step 1: Creating 2 vectors

 

We use combine function "c()" to create a vector

a = c(2,5,8,9,10) b = c(5,6,10)

Step 2: Element-wise comparison

 

We use relational operator greater than (">") to illustrate this operation. This gives us a boolean vector specifying whether the condition was met or not

print(a>b)

Warning message in a > b:
"longer object length is not a multiple of shorter object length"
[1] FALSE FALSE FALSE  TRUE  TRUE

 

Note: it gives a warning message ""longer object length is not a multiple of shorter object length" as well as it returns TRUE for every extra element that is present in the longer vector

 

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

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

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

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.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

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.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker