...

/

Functions in R

Functions in R

Get introduced to the functions available and the functions most commonly used in R.

An abundance of valuable functions

It’s impossible to try and provide a comprehensive list of the functions we can use in R. Here’s a brief list of our favorite and most used simple commands.

Functions for examining data frames or other objects

Function Syntax

Functionality

ncol()

This function tells us the number of columns in a data frame.

nrow()

This function tells us the number of rows in a data frame.

head()

This function shows us the top six rows of a data frame. Note that we can specify more rows if we want.

tail()

This function shows us the last six rows of a data frame. Note that we can specify more rows if we want.

names()

This function shows us the column names and gives us the option to change them.

colSums()

This function calculates the sums of multiple columns in a data frame.

colMeans()

This function calculates the means of multiple columns in a data frame.

General functions for creating or manipulating objects

Function Syntax

Functionality

seq()

This function creates a sequence of numbers. We can specify the start and end values of the sequence and how many values should be in between them.

rep()

This function repeats something a set number of times.

trunc()

This function truncates a number to just the integer.

round()

This function rounds a number to a set number of decimal places.

rbind()

This function binds two vectors together as rows.

cbind()

This function binds two vectors together as columns.

paste()

This function sticks two bits of text together. We can specify what character, if any, we want to use to separate them.

Mathematical functions

Function Syntax

Functionality

mean()

This function calculates the mean of a vector of numbers.

sum()

This function calculates the sum of a vector of numbers.

max()

This function finds the maximum value in a vector.

min()

This function finds the minimum value in a vector.

length()

This function tells us how long a vector is.

range()

This function tells us the maximum and minimum values of a vector.

sd()

This function calculates the standard deviation of a vector.

abs()

This function takes the absolute value of a vector.

Function usage

Most of the work done by R is going to be performed by functions. Later on, you will learn how to write your own. Functions are prewritten sets of code that we use to do something to a numerical value or set of values organized into an object. Functions can be straightforward, like calculating the average of a set of numbers, or very complicated. Functions are made up of two essential parts:

  • A name
  • A set of parentheses to specify the objects or values we want the function to work on.

Everything we type in the parentheses are called arguments.

The rnorm() function calculates a random number selected from a normal distribution. We can pass it a single number in its simplest form and see what happens:

R
# generating a random number from a normal distribution
rnorm(1)

The given code produces a single value, which may be positive or negative and isn’t too far from zero. What happens if we change the 1 to a different number? What happens if we execute the function a bunch of times? If we run that function over and over, we’ll notice a pattern, and we may start to wonder why the random values being generated are so close to zero. There are a number of hidden default values for the rnorm() function. How do we know what these values are? This is a good time to introduce the help function, which is just a simple ?. The quickest way is to just type a “?” at the prompt, followed by the thing we’re looking for:

R
# bring-up the help for `rnorm`
?rnorm

This brings up the help file for the function, which describes a variety of functions related to the normal distribution. In this case, we are only interested in rnorm().

Note: Interpreting a help file is a skill that we develop over time. The first time you see one, it’ll probably ...