What is lazy evaluation and how is it helpful in R

This recipe explains what is lazy evaluation and how is it helpful in R

Recipe Objective

Lazy evaluation also known as call by need is a technique where the expression's evaluation is delayed until it's value is absolutely needed. In other words, it's used to avoid repeated evluations.

Lazy evaluation is used in R as it increases the efficiency of the program when used interatively. This is done by making sure that only necessary objects are loaded in memory or looked for. One disadvantage is that it can make a program less predictable. ​

In this recipe we will see how lazy evaluation works in R with an example of a function ​

Master the Art of Classification in Machine Learning to Become a Pro

STEP 1: Create a function to evaluate mean of a vector

mean_of_vector = function(a, mean_ = mean(a)){ a = a[!is.na(a)] print(a) paste("The mean of a is ", mean_) }

STEP 2: Call the function

mean_of_vector(c(20,30,40,50,NA,60,NA))

[1] 20 30 40 50 60
'The mean of a is  40'

Note: In this example the the function changes the value of a before passing it to the mean_ function. If mean_ was evaluated as soon as the function was called then the value of mean_ would have been NA. This is Lazy evaluation. ​

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

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.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python