Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
R for Data Science Cookbook (n)

You're reading from   R for Data Science Cookbook (n) Over 100 hands-on recipes to effectively solve real-world data problems using the most popular R packages and techniques

Arrow left icon
Product type Paperback
Published in Jul 2016
Publisher
ISBN-13 9781784390815
Length 452 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Yu-Wei, Chiu (David Chiu) Yu-Wei, Chiu (David Chiu)
Author Profile Icon Yu-Wei, Chiu (David Chiu)
Yu-Wei, Chiu (David Chiu)
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Functions in R FREE CHAPTER 2. Data Extracting, Transforming, and Loading 3. Data Preprocessing and Preparation 4. Data Manipulation 5. Visualizing Data with ggplot2 6. Making Interactive Reports 7. Simulation from Probability Distributions 8. Statistical Inference in R 9. Rule and Pattern Mining with R 10. Time Series Mining with R 11. Supervised Machine Learning 12. Unsupervised Machine Learning Index

Performing lazy evaluation

R functions evaluate arguments lazily; the arguments are evaluated as they are needed. Thus, lazy evaluation reduces the time needed for computation. In the following recipe, we will demonstrate how lazy evaluation works.

Getting ready

Ensure that you completed the previous recipes by installing R on your operating system.

How to do it...

Perform the following steps to see how lazy evaluation works:

  1. First, we create a lazyfunc function with x and y as the argument, but only return x:
    >lazyfunc<- function(x, y){
    + x
    + }
    >lazyfunc(3)
    [1] 3
    
  2. On the other hand, if the function returns the summation of x and y but we do not pass y into the function, an error occurs:
    >lazyfunc2<- function(x, y){
    + x + y
    + }
    >lazyfunc2(3)
    Error in lazyfunc2(3) : argument "y" is missing, with no default
    
  3. We can also specify a default value to the y argument in the function but pass the x argument only to the function:
    >lazyfunc4<- function(x, y=2){
    + x + y
    + }
    >lazyfunc4(3)
    [1] 5
    
  4. In addition to this, we can use lazy evaluation to perform Fibonacci computation in a function:
    >fibonacci<- function(n){
    + if (n==0)
    + return(0)
    + if (n==1)
    + return(1)
    + return(fibonacci(n-1) + fibonacci(n-2))
    + }
    >fibonacci(10)
    [1] 55
    

How it works...

R performs a lazy evaluation to evaluate an expression if its value is needed. This type of evaluation strategy has the following three advantages:

  • It increases performance due to the avoidance of repeated evaluation
  • It recursively constructs an infinite data structure
  • It inherently includes iteration in its data structure

In this recipe, we demonstrate some lazy evaluation examples in the R code. In our first example, we create a function with two arguments, x and y, but return only x. Due to the characteristics of lazy evaluation, we can successfully obtain function returns even though we pass the value of x to the function. However, if the function return includes both x and y, as step 2 shows, we will get an error message because we only passed one value to the function. If we set a default value to y, then we do not necessarily need to pass both x and y to the function.

As lazy evaluation has the advantage of creating an infinite data structure without an infinite loop, we use a Fibonacci number generator as the example. Here, this function first creates an infinite list of Fibonacci numbers and then extracts the nth element from the list.

There's more...

Additionally, we can use the force function to check whether y exists:

>lazyfunc3<- function(x, y){
+ force(y)
+ x
+ }
>lazyfunc3(3)
Error in force(y) : argument "y" is missing, with no default
>input_function<- function(x, func){
+ func(x)
+ }
>input_function(1:10, sum)
[1] 55
You have been reading a chapter from
R for Data Science Cookbook (n)
Published in: Jul 2016
Publisher:
ISBN-13: 9781784390815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime