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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

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.

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.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.