Introduction

4

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.

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.

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()`

.

`1`

`abs(-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 `[1] 3`

.

And what if we input words?

`1`

`abs("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.

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).

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.

`1 2 3`

`summy <- function(a, b) { a + b }`

r

Functions in R generally have three parts:

- The
*formals*are the arguments you pass into the function. In the example above that's`a`

and`b`

. - The
*body*of the function is whatever code executes*inside*the function. Here`a + b`

. - 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.

`1`

`environment(summy)`

r

Note that it's part of the global environment. See here for much more.

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

`1 2`

`result <- abs(-10) result`

r

Often you'll call functions within functions like this:

`1`

`abs(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.

`1 2 3 4 5 6 7`

`fibonacci <- function(n) { if(n <= 1) { return(n) } else { return(fibonacci(n-1) + fibonacci(n-2)) } }`

r

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

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.

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.

`1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27`

`seq(from = 1, to = 9, by = 2) rep(x = 1:4, times = 2) sort(c(1,2,3,-1)) rev(c(9,4,5,6)) table(c(3,3,6,7,7,7)) unique(c(3,3,4,5,9,9,9)) substring(x = "abcdef", start = 2, stop = 4) gsub(x = "Hola, I think that", replacement = "Hola", pattern = "Hello") paste("1st", "2nd", "3rd", sep = " | ") paste0("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.

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")`

.

Three tiers of functions:

- Those from base R (i.e., those that came with any R installation).
- 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.

- Those you write yourself (see the
`summy`

example above).

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.

4