Functions are the verbs of R. When you do something in R, you do it by running a function. For example,

  • To take a log, you run a function named log: log(2.71).
  • To fit a linear model, you run a function named lm: lm(mpg ~ wt, data = mtcars).
  • To add two numbers, you run a function named “+”: 1 + 2.

This last one is an alternative way to call the function '+'(1, 2). In R, even things that do not look like functions are functions.

But what is a function?

A function is simply a piece of code that is packaged in a way that makes it easy to reuse.

This short tutorial will show you how R functions are packaged and how you can work with R functions. It is a pre-requisite for the next tutorial, which will show you how to write your own R functions. Let’s begin!