The Rundown on Packages in R

Packages are the backbone of what makes the R programming language great. Both the huge number of options and the high quality of the packages makes it easy to integrate into your own work.

To install a package, all that is required is the package’s name and the following function call:

install.packages(“package name”)

To view the list of packages already installed on this machine:

library()

However, installing a package does not make it readily avaliable to use in your code. To load a package up for use, the require function is needed:

require(“package name”)

And to view a list of the packages already loaded and available to use:

search()

The packages you should know about

dplyr is a great for general data manipulation and selection. It offers many useful functions such as filter, aggregate and select. But also it adds the %>% operator for chaining commands together so they appear in sequential order in your code. For example:

select(arrange(filter(trees, Girth > 10), Height), Height, Volume)

Can be converted into:

trees %>%
filter(Girth > 10) %>%
arrange(Height) %>%
select(Height, Volume)

Essentially anything of the form f(x,y) can be converted into x %>% f(y).

Read more about dplyr here.

lubridate becomes useful anytime there are dates or times in your data. Usually these can cause headaches and lubridate aims to make this a little bit easier. Parsing a particular date usually gets tricky but with lubridate, just call the function with the correct year, month, day, hour, minute and second ordering. For example:

dirty.date <- "2/19/2017 11:37"
clean.date <- mdy_hm(dirty.date)

Once parsed, lubridate also offers numerous functions for extracting and manipulating dates.

Read more about lubridate here.

ggplot2 is the go-to package for graphing and data visualization. With hundreds to thousands of options and customizations, any type of visualization can be done with ggplot2.

Read more about ggplot2 here.

rmarkdown enables developers to write reports and documents from within R. This totally bypasses the usual laborius process of exporting and importing the data into another reporting tool. By writing reports in R, you can use other R packages in your reports making it easy to include data, graphs and visualizations.

Read more about rmarkdown here.

For futher reading, see RStudio’s Quick list of useful R packages.