Pipes and functions in R
Learning objectives
- Demonstrate how to use the pipe
- Demonstrate piping alternatives
- Define functions and their purpose
- Analyze a user-written function and explain how it works
- Demonstrate conditional execution
- Practice writing functions
Notes
April 27, 2020
Pipes in R
- Think back to the
diamonds
dataset
filter()
, group()
, and then summarize()
Approaches to Coding
diamonds_# <- function
- Unclear because the names are not intuitive
- Might forget what the name of the object means
- Make spelling mistakes
- Might start running out of memory (not as common)
- This works, but there are a couple of concerns when writing code this way all the time
Overwrite the original
- If something goes wrong, you basically have to start over adn retrieve the original dataset
Function Composition
- Now we're writing code in sort of a backwards manner
- Again, not very intuitive
All of these approaches produce the exact same output, but we want to make sure that our code is interpretable and reproducible_
Piping
- A lot more intuitive
- Implicit
- Remember: You don't assign
<-
something to an object inside the pipe operator
- Depending on complexicity of task, you may have a lot of steps
- Break piped operation into a smaller set of tasks and save output
- Next line of code is automaticaly indented when you are using a piped operator
- For all of the
tidyverse
functions we use the %>%
at the end of a line.
ggplot2
is different in that it uses a plus symbole +
The %>%
comes from the maggritr
package. There are other piping operators in the package, including the "tee" pipe %T%
and %<>%
Functions in R
- Easy to use
- Self-documenting
- Easy-ier to debug
- If you have copied and pasted a block of code more than twice, convert it to a function
Rescale Function
How to use a function
- When using functions, by default the returned object is merely printed to the screen
Conditional execution
- Recoding variables is where you would use
if()
statements
- Need a vectorized statement check for multiple conditions
FIZZBUZZ