Install Packages in R : A Quick Tutorial

In this tutorial, we’ll learn how to install packages in R.

We’ll start with what R packages are and then learn how to install packages in R.  After that we’ll discuss how to use R packages and finally we’ll learn about some useful packages and commands.

Packages in R

Packages are collections of R functions, data and compiled code in a well-defined format.  The directory where packages are stored is called a library.  Packages include functions, data examples, help files, descriptions and namespaces.  

A core set of packages is included when you install R, but additional packages are required for specific requirements.  These extend the functionality of R by enabling additional visual capabilities, statistical methods, and discipline-specific functions, to name just a few. 

Packages can be downloaded from various repositories such as CRAN (the Comprehensive R Archive Network), Github, Bioconductor, R Forge and many more.

R package contents

Now let’s take a look at installing an R package.  In this example, we have downloaded a package from CRAN.

install.packages(“pkg”),  is used to install a package where pkg is the name of the package.

Suppose we want to install the “dplyr” package,  then the command used will be install.packages(“dplyr”)

Install packages in R

After we’ve installed the package using the install.packages(“pkg”) command,  the next step is to load it.

The library(<pkg>) command is used to load the required package into the ongoing session.  Staying with our example, we invoke the library(dplyr) command to load the package into our ongoing session.

How to use R Package

Some commonly used commands are remove.packages(“pkg”),  used to remove a specific package , update.packages(), used to update all installed packages , installed.packages(), which provides a list of all installed packages and available.packages(), which returns a list of all available packages.

Removing Packages

 remove.packages(“pkg”)  

Removes a specific package

Updating Packages

Updates all installed packages

Installed Packages

 installed.packages

 Returns a list of all installed packages

Finding Available Packages

available.packages()

Returns a list of all available packages

Some commonly used packages for loading data in an R session are readr, data.table, foreign and Hmisc. 

data.table , dplyr, reshape2 and tidyr are packages used for data management. You can learn more about data management in our article about dataframe in r.

The ggplot2 package is widely used for data visualization.

car, caret, e1071, party and ROCR are  some very important packages used while performing predictive modelling in R.

For Time series Analysis , packages such as forecast and zoo are used.

Common R Packages

We can now quickly recap what we have covered in this tutorial.

Firstly, we looked at what a package is, then we installed and loaded an R package and lastly we learned about some commonly used commands and packages.

R Commands

This tutorial lesson is taken from the Postgraduate Diploma in Data Science.