Liwaiwai Liwaiwai
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • About
Liwaiwai Liwaiwai
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
    • Architecture
    • Design
    • Software
    • Hybrid Cloud
    • Data
  • About
  • Data
  • Development

The Pipe Operator : A Better Way To Nest Functions In R

  • December 12, 2019
  • admin

During your data manipulation, exploration, and even analysis, functions can get complicated, putting one function inside of one another in order to accomplish tasks in one swoop. These are called nested functions.

A nested function may look like something like this:

function_1(function_2(function_3(function_4(x))))

Not only can this confuse you while you are coding, this looks quite ugly in the eyes. When you forget to add an operation, you will have to scour a flood of parentheses. What a nightmare.

In R, a better way of dealing with nested functions is the pipe operator, %>%. How is this function used? Let’s boil it down.

Requirements

  • R (>= 3.1.2)
  • dplyr package

Preliminaries

If you haven’t yet, install the magrittr or dplyr package in R by executing either of the two succeeding lines of code.

install.packages(“dplyr”)

 

Afterwards, load the package that you have installed using the appropriate line below:

library(dplyr)

Using the pipe operator

The pipe operator, %>% passes the left-hand side argument as the first argument right-hand side function.

For demonstration, let’s create a data frame, x by executing the following line:

x <- -5:5
x

 

Now, this will generate the following output:

> x
[1] -5 -4 -3 -2 -1 0 1 2 3 4 5

 

What if, just as an example, you have to get the mean of the absolute difference of the data values and the mean?

That’s confusing, let’s decompose it:

  1. We first get the difference of the values of x and the mean of x, in other words, x – mean(x),
  2. We compute the absolute value of the difference above, i.e., |x – mean(x)|
  3. We compute the mean of the absolute differences, mean {|x – mean(x)|}
Read More  Synthetic Data’s Role In The Future Of AI

We can perform this using the following nested function:

mean(abs(x-mean(x)))

[1] 2.727273

Realistically, this isn’t so bad, but when we add more functions in, things will start to get crazy.

Using the pipe operator, we can systematize the flow of the operations in accord to the way  we typically think:

1. We first get the difference of the values of x and the mean of x, in other words, x – mean(x),

(x - mean(x))

2. We compute the absolute value of the difference above, i.e., |x – mean(x)|

(x - mean(x)) %>% abs

3. We compute the mean of the absolute differences, mean {|x – mean(x)|}

(x - mean(x)) %>% abs %>% mean

[1] 2.727273

We see that the results are just the same as the nested function.

Instead of going from the innermost function to the outermost function, the pipe operator shifts the thinking of the operations from left to right — in the sequence that our minds usually operates in.

With the structure of the pipe operator, we can see that we can easily add more operations in case we need to do more.

And that’s about it. The application of the pipe operator is extremely versatile. You can use it not only for vectors as shown here. You can use it for other R objects which you intend to pass in multiple operations in one go.

admin

Related Topics
  • Code
  • Pipe Operator
  • R
You May Also Like
View Post
  • Big Data
  • Data
  • Design

From Raw Data To Actionable Insights: The Power Of Data Aggregation

  • March 30, 2023
View Post
  • Data
  • Design
  • Engineering

Effective Strategies To Closing The Data-Value Gap

  • March 30, 2023
View Post
  • Artificial Intelligence
  • Data
  • Data Science
  • Machine Learning
  • Technology

Google Data Cloud & AI Summit : In Less Than 12 Hours From Now

  • March 29, 2023
View Post
  • Data
  • Machine Learning
  • Platforms

Coop Reduces Food Waste By Forecasting With Google’s AI And Data Cloud

  • March 23, 2023
View Post
  • Data
  • Engineering

BigQuery Under The Hood: Behind The Serverless Storage And Query Optimizations That Supercharge Performance

  • March 22, 2023
View Post
  • Data
  • Design
  • Engineering
  • Tools

Sumitovant More Than Doubles Its Research Output In Its Quest To Save Lives

  • March 21, 2023
View Post
  • Data
  • Platforms
  • Technology

How Osmo Is Digitizing Smell With Google Cloud AI Technology

  • March 20, 2023
View Post
  • Data
  • Engineering
  • Tools

Built With BigQuery: How Sift Delivers Fraud Detection Workflow Backtesting At Scale

  • March 20, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay Connected!
LATEST
  • 1
    Bard And ChatGPT — A Head To Head Comparison
    • March 31, 2023
  • 2
    Modernize Your Apps And Accelerate Business Growth With AI
    • March 31, 2023
  • 3
    Why Your Open Source Project Needs A Content Strategy
    • March 31, 2023
  • 4
    From Raw Data To Actionable Insights: The Power Of Data Aggregation
    • March 30, 2023
  • 5
    Effective Strategies To Closing The Data-Value Gap
    • March 30, 2023
  • 6
    Unlocking The Secrets Of ChatGPT: Tips And Tricks For Optimizing Your AI Prompts
    • March 29, 2023
  • 7
    Try Bard And Share Your Feedback
    • March 29, 2023
  • 8
    Google Data Cloud & AI Summit : In Less Than 12 Hours From Now
    • March 29, 2023
  • 9
    Talking Cars: The Role Of Conversational AI In Shaping The Future Of Automobiles
    • March 28, 2023
  • 10
    Document AI Introduces Powerful New Custom Document Classifier To Automate Document Processing
    • March 28, 2023

about
About
Hello World!

We are liwaiwai.com. Created by programmers for programmers.

Our site aims to provide materials, guides, programming how-tos, and resources relating to artificial intelligence, machine learning and the likes.

We would like to hear from you.

If you have any questions, enquiries or would like to sponsor content, kindly reach out to us at:

[email protected]

Live long & prosper!
Most Popular
  • 1
    Introducing GPT-4 in Azure OpenAI Service
    • March 21, 2023
  • 2
    How AI Can Improve Digital Security
    • March 27, 2023
  • 3
    ChatGPT 4.0 Finally Gets A Joke
    • March 27, 2023
  • 4
    Mr. Cooper Is Improving The Home-buyer Experience With AI And ML
    • March 24, 2023
  • 5
    My First Pull Request At Age 14
    • March 24, 2023
  • /
  • Artificial Intelligence
  • Machine Learning
  • Robotics
  • Engineering
  • About

Input your search keywords and press Enter.