Writing Custom Functions
In our Introduction to Programming in R lesson, you learned that functions contribute to efficient programming. Up until this point, you have been exposed to pre-written R functions are written in a compiled language. In this lesson, you'll learn to create your own single and multivariable functions, and you’ll learn when to write a function versus using an existing one.
Functions allow you to write a piece of abstract code and reuse it over and over by calling the function each time you want to use it. This can cut down on programming time so you don't have to perform the same actions repeatedly.
In this lesson, you'll learn to write your own custom functions in R. Functions are extremely versatile and can use as many parameters as you desire. You will learn the anatomy of a function: a body (code inside the function), arguments (list of inputs that control the function), and environment (the location where the function is executed). You will also learn how to write a custom function on your own to perform a specific task.
Then you’ll learn to incorporate control structures in your functions to specify conditional execution of commands. You’ll also learn about using if-else statements in your functions to specify different actions under different conditions.
1. Introduction to Writing Your Own Functions
2. Anatomy of a Function
3. When to Write a Function
4. Writing Functions with Two Variables as Arguments
5. Writing Functions for Conditional Execution
6. Functions with More Than Two Arguments
7. Next Steps