Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
R for Data Science Cookbook (n)

You're reading from  R for Data Science Cookbook (n)

Product type Book
Published in Jul 2016
Publisher
ISBN-13 9781784390815
Pages 452 pages
Edition 1st Edition
Languages
Author (1):
Yu-Wei, Chiu (David Chiu) Yu-Wei, Chiu (David Chiu)
Profile icon Yu-Wei, Chiu (David Chiu)

Table of Contents (19) Chapters

R for Data Science Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Functions in R 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

Matching arguments


In R functions, the arguments are the input variables supplied when you invoke the function. We can pass the argument, named argument, argument with default variable, or unspecific numbers of argument into functions. In this recipe, we will demonstrate how to pass different kinds of arguments to our defined function.

Getting ready

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

How to do it...

Perform the following steps to create a function with different types of argument lists:

  1. Type the following code to your R console to create a function with a default value:

    >defaultarg<- function(x, y = 5){
    + y <- y * 2
    + s <- x+y
    + return(s)
    + }
    
  2. Then, execute the defaultarg user-defined function by passing 3 as the input argument:

    >defaultarg(3)
    [1] 13
    
  3. Alternatively, you can pass different types of input argument to the function:

    >defaultarg(1:3)
    [1] 11 12 13
    
  4. You can also pass two arguments into the function:

    >defaultarg(3,6)
    [1] 15
    
  5. Or, you can pass a named argument list into the function:

    >defaultarg(y = 6, x = 3)
    [1] 15
    
  6. Moving on, you can use if-else-condition together with the function of the named argument:

    >funcarg<- function(x, y, type= "sum"){
    + if (type == "sum"){
    + sum(x,y)
    + }else if (type == "mean"){
    + mean(x,y)
    + }else{
    + x * y
    + }
    + }
    >funcarg(3,5)
    [1] 8
    >funcarg(3,5, type = 'mean')
    [1] 3
    >funcarg(3,5, type = 'unknown')
    [1] 15
    
  7. Additionally, one can pass an unspecified number of parameters to the function:

    >unspecarg<- function(x, y, ...){
    + x <- x + 2
    + y <- y * 2
    + sum(x,y, ...)
    + }
    >unspecarg(3,5)
    [1] 15
    >unspecarg(3,5,7,9,11)
    [1] 42
    

How it works...

R provides a flexible argument binding mechanism when creating functions. In this recipe, we first create a function called defaultag with two formal arguments: x and y. Here, the y argument has a default value, defined as 5. Then, when we make a function call by passing 3 to defaultarg, it passes 3 to x and 5 to y in the function and returns 13. Besides passing a scalar as the function input, we can pass a vector (or any other data type) to the function. In this example, if we pass a 1:3 vector to defaultarg, it returns a vector.

Moving on, we can see how arguments bind to the function. When calling a function by passing an argument without a parameter name, the function binds the passing value by position. Take step 4 as an example; the first 3 argument matches to x, and 6 matches to y, and it returns 15. On the other hand, you can pass arguments by name. In step 5, we can pass named arguments to the function in any order. Thus, if we pass y=6 and x=3 to defaultarg, the function returns 15.

Furthermore, we can use an argument as a control statement. In step 6, we specify three formal arguments: x, y, and type, in which the type argument has the default value defined as sum. Next, we can specify the value for the type argument as a condition in the if-else control flow. That is, when we pass sum to type, it returns the summation of x and y. When we pass mean to type, it returns the average of x and y. When we pass any value other than sum and mean to the type argument, it returns the product of x and y.

Lastly, we can pass an unspecified number of arguments to the function using the ... notation. In the final step of this example, if we pass only 3 and 5 to the function, the function first passes 3 to x and 5 to y. Then, the function adds 2 to x, multiplies y by 2, and sums the value of both x and y. However, if we pass more than two arguments to the function, the function will also sum the additional parameters.

There's more...

In addition to giving a full argument name, we can abbreviate the argument name when making a function call:

>funcarg(3,5, t = 'unknown')
[1] 15

Here, though we do not specify the argument's name, type, correctly, the function passes a value of unknown to the argument type, and returns 15 as the output.

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 $15.99/month. Cancel anytime}