Section 9 Packages

R has a large number of packages available (over 16,000 at the time of writing). R packages typically contain extra functions based around some theme (e.g. a collection of functions to enable you to fit a particular type of statistical model.)

Some packages come pre-installed with R, and others need to be installed manually.

9.1 Installing a package

Use the command install.packages() to install a package. If the package you want requires other packages, R will install them too, so installation can take a while! During the installation process, you may get a message along the lines of

...There are binary versions available but the source versions are later. Do you want to install from sources the package which needs compilation?

Answering n (for no) is usually safe, in case your computer isn’t set up for installing packages ‘from source’.

As an example, we’ll install the zoo package, which has some helpful functions for working with time series data.

install.packages("zoo")

Do not put an install.packages() command in a script or R Markdown document: use the command in the console only. (You do not want to be repeatedly installing a package, or should you share a script/R Markdown document with someone else, for your documents to be installing software on other people’s computers.)

9.2 Updating packages

It’s worth occasionally checking for package updates. In particular, if a package you’re using doesn’t appear to be working properly, look to see if there’s an update. In RStudio, click on the Packages tab, and then click on Update to see what’s available.

9.3 Using packages

A package only needs to be installed once, but you need to load the package every time you start R, or use the :: syntax. For example, the zoo package has a function rollmean() for computing rolling means (e.g. mean at time \(t\) of the most recent 4 observations up to time \(t\)). As an example, suppose we have a vector x:

x <- 10:20

We can either use the syntax package-name::function.

zoo::rollmean(x, k = 4)
## [1] 11.5 12.5 13.5 14.5 15.5 16.5 17.5 18.5

or we can first do

library(zoo)
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric

and then any function within the package can be used directly:

rollmean(x, k = 4)
## [1] 11.5 12.5 13.5 14.5 15.5 16.5 17.5 18.5

For some packages (e.g. ggplot2), it will be obvious from the commands what package is being used. Otherwise, I recommend you use the :: syntax as it will make your code easier to read; it can be helpful to be explicit about which package a function has come from.

9.4 Is there a package for…?

If you’re searching for a package to do something, one simple approach is to include “R CRAN” in your web search, e.g. you might search for R CRAN lasso if you were looking for a package to implement the lasso. (CRAN is the main repository for R packages).

You could also try browsing the CRAN Task Views. These discuss some of the packages that are available for particular topics (e.g Bayesian inference, machine learning, time series analysis etc.)