R Basics

7. Packages

R is a versatile piece of kit straight out of the box. By itself it can do just about anything, from simple calculations, to automating a Twitter account, through to being a robot’s ‘brain’. However, we can make most jobs that we will do in R a lot easier by using packages – sort of like R add-on kits. These packages create groups of functions that make tasks quicker to write and perform. Without them, we’d have to write horribly lengthy code once we got beyond simple tasks.

To use a packages, you must first install it then make sure the package is loaded into any code that will use it. This article will take you through both steps:

Installing packages


It is very easy to install packages within R with the function install.packages("name_of_package")

          
    # example

    install.packages("dplyr")
          
  

Loading packages


There are two ways of loading packages via the require() and the library() functions. The only difference is that if the package is not installed the require() function will output a warning whereas library will throw an error. I prefer require() but you can chose your own tools.

          
    # example

    require(dplyr)
          
  

Summary


Harvard’s Introduction to Computer Science course opens with the discussion that we are ‘standing on the shoulders of giants’, highlighting the work of programmers before us that have built languages, modules and tools that allow us to be more productive.The R community is a perfect example of this, with thousands of packages available to us that make complex tasks a bit more manageable.

See some of these packages in action across data analysis, visualisation and web scraping.

Next Lessons...


  1. Numbers, Variables & Strings
  2. Comparisons & Logic
  3. If Statements
  4. For Loops
  5. While Loops
  6. Lists
  7. Packages
  8. Creating Functions
  9. Random Numbers with Expected Goals

This is a R conversion of a tutorial by FC Python. I take no credit for the idea and have their blessing to make this conversion. All text is a direct copy unless changes were relevant. Please follow them on twitter and if you have a desire to learn Python then they are a fantastic resource!