 Levi Thatcher

• Jun 11, 2019
• 727 Views
• Jun 11, 2019
• 727 Views
Data
R

## Introduction

As in many programming languages, functions are foundational to R. Some say that at its heart, R is a functional programming language (using the formal definition). This makes it very easy to not repeat yourself and to build on the work of others. Out of the box, R comes with a large assortment of functions that let you do an inordinate amount of rigorous work. This is from base R. On top of it, the community has created thousands of packages that extend the functionality of base R in many elegant and niche ways. For this guide on applying built-in functions, we'll focus on base R, which is what you work with when first installing R. For those of you who are new, R is the engine and RStudio is the most common IDE.

For those who are curious, we'll build this via R v3.5.1, RStudio v1.1.463, and R Markdown.

## Functions in R

Note that functions in R mimic behaviors in many other languages. R, however, goes a step further than most in that R functions are first class citizens, which means that you can do anything with them or to them that you can with vectors.

### Executing a Function

To start simply, let's first call a function. For those that are new to programming and/or R, a function operates on some input(s) to accomplish a specific task. The best functions are short and specific. You provide it something, it does some computation and returns something to you. Occasionally a function won't technically return anything but could do something like print - in this Guide, we'll focus on functions that return something.

Let's start with a built-in function that lives in the base R package - `abs()`, which takes a number and returns its absolute value. If the input is negative, it returns positive; if the input is positive, it returns positive.

One of the few tips to calling a function successfully is to provide input that it can work with. In the case of our simple `abs()` function above, that means providing numbers. If your input is invalid, the engine won't turn. Note the simple syntax for calling `abs()`.

``1abs(-3)``
r Note that when returning values from a function, R specifieswhether the number associated with the output is the first value output, which is why it says ` 3`.

And what if we input words?

``1abs("male")``
r As mentioned, if you don't input what's expected, you have no guarantees. Let's face it, R is opensource, so you don't really have any guarantees anyhow (though it typically works like a dream). Depending on the package you're calling - and base R is quite well written - you may receive robust or little help determining what went wrong with your function call.

### Getting Help

When calling a function, you may run into one of two main issues: 1. The function does not run; or 2. The function doesn't return what you'd expect.

Let's say you're confused by `abs()`. The easiest way to get help and check that you're passing the correct arguments is to type `?abs()` in the RStudio Console or Terminal (depending on where you're working).

## Components of a Function in R

### Defining a Function

Let's quickly show you what a function definition looks like in R. Then, we'll run through its components and how it's applied.

Note that while this Guide focuses on built-in functions, we'll show you how a function is defined here such that we can talk about its components.

``````1summy <- function(a, b) {
2  a + b
3}``````
r

Functions in R generally have three parts:

1. The formals are the arguments you pass into the function. In the example above that's `a` and `b`.
2. The body of the function is whatever code executes inside the function. Here `a + b`.
3. The environment of the function is the other consideration. Each of these is actually a function as well. To find the environment of `summy` we simply call the `environment` function. This shows we can pass functions as arguments to other functions.
``1environment(summy)``
r Note that it's part of the global environment. See here for much more.

## What Else Do I Need to Know About Functions?

Of course, most of the time you won't have functions print to the Console, but will rather assign them to variables.

``````1result <- abs(-10)
2result``````
r Often you'll call functions within functions like this:

``1abs(min(c(-5, -3, -1)))``
r Occasionally, you'll call functions recursively. That is, within a function, you'll call that same function to solve some problem. Fibonacci numbers are a popular example of how this works.

``````1fibonacci <- function(n) {
2  if(n <= 1) {
3  return(n)
4  } else {
5  return(fibonacci(n-1) + fibonacci(n-2))
6  }
7}``````
r

If you're new to programming, recursive functions are a little advanced, but fun to noodle over.

## How Can I Find R's Built-in Functions?

### What's Included out of the Box?

When installing R, there are 15 packages installed and loaded as part of the typical R installation (one of which is called `base`).

The `base` package includes hundreds of functions. The `stats` provides roughly one hundred more and is a great way to evaluate your familiarity with the world of stats.

### The Tippy Top of Helpfulness

While there are thousands of functions you could use, here are some that will help you understand what R is all about. For an exercise, pick five and make sure you know how these functions are used. Note that the `c()` syntax is how you create a vector of numbers in R.

``````1seq(from = 1,
2    to = 9,
3    by = 2)
4
5rep(x = 1:4,
6    times = 2)
7
8sort(c(1,2,3,-1))
9
10rev(c(9,4,5,6))
11
12table(c(3,3,6,7,7,7))
13
14unique(c(3,3,4,5,9,9,9))
15
16substring(x = "abcdef",
17          start = 2,
18          stop = 4)
19
20gsub(x = "Hola, I think that",
21     replacement = "Hola",
22     pattern = "Hello")
23
24paste("1st", "2nd", "3rd",
25      sep = " | ")
26
27paste0("1st", "2nd")``````
r

These functions are not only foundational to R work, they'll also help you decide if R is the right tool for the job at hand.

### Searching via Code/RStudio

The built-in R help not only works for individual functions (via `?abs()`), but also for entire packages as well. In RStudio, type `?base` to learn what it's for. To see its full list of packages, use `library(help = "base")`.

## What Other Functions Should I Know About?

Three tiers of functions:

1. Those from base R (i.e., those that came with any R installation).
2. Those from libraries you've installed (via `install.packages('pkgname')`).

Note: there are thousands of packages in the R universe; here's a list of the most popular.

3. Those you write yourself (see the `summy` example above).

## Conclusion

If you're excited to work in R, functions are your bread and butter. They keep your code organized, reusable, and testable. Being able to use these in a deliberate way makes your data toolbelt (and you) much more valuable.