Book Image

R for Data Science Cookbook (n)

By : Yu-Wei, Chiu (David Chiu)
Book Image

R for Data Science Cookbook (n)

By: Yu-Wei, Chiu (David Chiu)

Overview of this book

This cookbook offers a range of data analysis samples in simple and straightforward R code, providing step-by-step resources and time-saving methods to help you solve data problems efficiently. The first section deals with how to create R functions to avoid the unnecessary duplication of code. You will learn how to prepare, process, and perform sophisticated ETL for heterogeneous data sources with R packages. An example of data manipulation is provided, illustrating how to use the “dplyr” and “data.table” packages to efficiently process larger data structures. We also focus on “ggplot2” and show you how to create advanced figures for data exploration. In addition, you will learn how to build an interactive report using the “ggvis” package. Later chapters offer insight into time series analysis on financial data, while there is detailed information on the hot topic of machine learning, including data classification, regression, clustering, association rule mining, and dimension reduction. By the end of this book, you will understand how to resolve issues and will be able to comfortably offer solutions to problems encountered while performing data analysis.
Table of Contents (19 chapters)
R for Data Science Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating infix operators


In the previous recipe, we learned how to create a user-defined function. Most of the functions that we mentioned so far are prefix functions, where the arguments are in between the parenthesis after the function name. However, this type of syntax makes a simple binary operation of two variables harder to read as we are more familiar with placing an operator in between two variables. To solve the concern, we will show you how to create an infix operator in this recipe.

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 an infix operator in R:

  1. First, let's take a look at how to transform infix operation to prefix operation:

    > 3 + 5
    [1] 8
    > '+'(3,5)
    [1] 8
    
  2. Furthermore, we can look at a more advanced example of the transformation:

    > 3:5 * 2 - 1
    [1] 5 7 9
    > '-'('*'(3:5, 2), 1)
    [1] 5 7 9
    
  3. Moving on, we can create our infix function that finds the intersection between two vectors:

    >x <-c(1,2,3,3, 2)
    >y <-c(2,5)
    > '%match%' <- function(a,b){
    + intersect(a, b)
    + }
    >x %match% y
    [1] 3
    
  4. Let's also create a %diff% infix to extract the set difference between two vectors:

    > '%diff%' <- function(a,b){
    + setdiff(a, b)
    + }
    >x %diff% y
    [1] 1 2
    
  5. Lastly, we can use the infix operator to extract the intersection of three vectors. Or, we can use the Reduce function to apply the operation to the list:

    >x %match% y %match% z
    [1] 3
    > s <- list(x,y,z)
    >Reduce('%match%',s)
    [1] 3
    

How it works...

In a standard function, if we want to perform some operations on the a and b variables, we would probably create a function in the form of func(a,b). While this form is the standard function syntax, it is harder to read than regular mathematical notation, (that is, a * b). However, we can create an infix operator to simplify the function syntax.

Before creating our infix operator, we examine different syntax when we apply a binary operator on two variables. In the first step, we demonstrate how to perform arithmetic operations with binary operators. Similar to a standard mathematical formula, all we need to do is to place a binary operator between two variables. On the other hand, we can transform the representation from infix form to prefix form. Like a standard function, we can use the binary operator as the function name, and then we can place the variables in between the parentheses.

In addition to using a predefined infix operator in R, the user can define the infix operator. To create an operator, we need to name the function that starts and ends with %, and surround the name with a single quote (') or back tick (`). Here, we create an infix operator named %match% to extract the interaction between two vectors. We can also create another infix function named %diff% to extract the set difference between two vectors. Lastly, though we can apply the created infix function to more than two vectors, we can use the reduce function to apply the %match% operation on the list.

There's more...

We can also overwrite the existing operator by creating an infix operator with the same name:

>'+' <- function(x,y) paste(x,y, sep = "|")
> x = '123'
> y = '456'
>x+y
[1] "123|456"

Here, we can concatenate two strings with the + operator.