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

Using the replacement function

On some occasions in R, we may discover that we can assign a value to a function call, which is what the replacement function does. Here, we will show you how the replacement function works, and how to create your own.

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 create a replacement function in R:

  1. First, we assign names to data with the names function:
    > x <- c(1,2,3)
    >names(x) <- c('a','b','c')
    >x
    a b c
    1 2 3
    

    What the names function actually does is similar to the following commands:

    > x <- 'names<-'(x,value=c('a','b','c'))
    >x
    a b c
    1 2 3
    
  2. Here, we can also make our replacement function:
    > x<-c(1,2,3)
    > "erase<-" <- function(x, value){
    + x[!x %in% value]
    + }
    >erase(x) <- 2
    >x
    [1] 1 3
    
  3. We can invoke the erase function in the same way that we invoke the normal function:
    >x <- c(1,2,3)
    > x <- 'erase<-'(x,value=c(2))
    >x
    [1] 1 3
    

    We can also remove multiple values with the erase function:

    > x <- c(1,2,3)
    >erase(x) = c(1,3)
    >x
    [1] 2
    
  4. Finally, we can create a replacement function that can remove values of certain positions:
    > x <- c(1,2,3)
    > y <- c(2,2,3)
    > z <- c(3,3,1)
    > a = list(x,y,z)
    > "erase<-" <- function(x, pos, value){
    + x[[pos]] <- x[[pos]][!x[[pos]] %in% value]
    + x
    + }
    >erase(a, 2) = c(2)
    >a
    [[1]]
    [1] 1 2 3
    [[2]]
    [1] 3
    [[3]]
    [1] 3 3 1
    

How it works...

In this recipe, we first demonstrated how we could use the names function to assign argument names for each value. This type of function method may appear confusing, but it is actually what the replacement function does: assigning the value to the function call. We then illustrated how this function works in a standard function form, which we achieved by placing an assignment arrow (<-) after the function name and placing the x object and value between the parentheses.

Next, we learned how to create our replacement function. We made a function named erase, which removed certain values from a given object. We invoked the function by wrapping the vector to replace within the erase function and assigning the value to remove on the right-hand side of the assignment notation. Alternatively, we can still call the replacement function by placing an assignment arrow after erase as the function name. In addition to removing a single value from a given vector object, we can also remove multiple values by placing a vector on the right-hand side of the assignment function.

Furthermore, we can remove the values of certain positions with the replacement function. Here, we only needed to add a position argument between the object and value within the parentheses. As our last step shows, we removed 2 from the second value of the list with our newly-created replacement function.

There's more...

As mentioned earlier, names<- is a replacement function. To examine whether a function is a replacement function or not, use the get function:

>get("names<-")
function (x, value) .Primitive("names<-")
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