Rainar Angelo

View Original

Day 10: What do Bob the builder and R have in common?

Bobbbb the builder can we fix it?

Bobbbb the builder yes we --

Oh... hello 

Didn't see you there

I was remembering one of the greatest cartoons of all time (based on nostalgic value) 🎶

"Why?" you ask?

That's easy...

Earlier today I was reading about pipes

And I started imagining pipes...

Who works with pipes?

A plumber... and Bob the builder was a plumber

Pipes... plumber... you see the connection

If you're slapping your hand to your forehead I do NOT apologise for it (😆)

but...

Speaking about pipes

It's going to be one of the MOST mega, uber, super useful thing ever...

It looks like this

" | > " and it's also read as 'then'

What does it do?

In simple terms... it carries what's on its 'left' and passes it along to the function on it's right

What does this look like?

flights |>
    filter(dep_delay>110)

(Remember to use yesterday's data set 'flights' from the nycflights13 package.)

But pipes doesn't show it's full power here

Here's when it becomes even more useful

flights |>
  filter(dest == "IAH") |>
   mutate(speed = distance / air_time * 60)

What does this look like without using pipe?

mutate(
    filter(
        flights,
        dest == "IAH"
    ),
speed = distance / air_time * 60
    )

They both do exactly the same thing

Your tibble should show 7198*20 rows

Pipe or |> makes it easier and logical to write the code in the exact way you want to filter out the data.

Some of you might be wondering...

"Isn't pipe %>%?"

You are right, but, since R 4.1 was introduced, there has been a pipe function in the base package itself.

More on %>% in the future :)

I hope this was easier to understand that it looked in the code

Feel free to shoot any questions you have

(and if you're new, don't hesitate to ask me anything about the emails or related to R)