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:
- 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
- Here, we can also make our replacement function:
> x<-c(1,2,3) > "erase<-" ...
Get R for Data Science Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.