2.3 Packages

Essential for interacting with R, packages are a collection of user-made functions to carry out particular tasks. A function can be as simple as returning a mean of some data, or as complex as processing data and running a statistical algorithm. The key intuition is that functions are collections of code that can execute a particular task. Functions normally take one or more inputs and provide one or more outputs. The collection of functions included in R before loading any packages is called ‘Base R’.

To load the functions in a given package, we first have to install the package. We do this using the install.packages() function. Run the line of code that installs the tidyverse package below by removing the # at the start of the second line to ‘uncomment’ the code. R will install the package to a default directory on your computer. If any dialogue box prompts you to ‘set up a personal library instead’, click yes. You’ll hear a bit more about functions in the session.

#--- Install the package
# install.packages("tidyverse", dependencies = T)

Once we have the package installed, we must load the functions from this library so we can use them within R. Load the library with the below code.

#--- Load library
 library(tidyverse)

The tidyverse contains a number of highly useful functions for visualising, summarising, tidying, and modelling data - the four things we will be doing over the course of these practical workshops. We also need some data to get going with so:

EXERCISE: Install the ‘gapminder’ package and load its library.

Once you have installed both the tidyverse and gapminder packages and loaded their libraries, run the following line of code and then fill in THIS GOOGLE FORM - please include your answer to the question of ‘What was the average life expectancy in Europe in 2007?’ Take a second to hypothesise what each line of this code is doing - we will explore these functions in due course.

#--- Find out the average life expectancy in 2007 by continent
gapminder %>% 
  filter(year == 2007) %>% 
  group_by(continent) %>% 
  summarise(mean = mean(lifeExp))

Congratulations, you’ve successfully gotten set up and run your first R commands! If you have run into any problems along the way, please email or come to the first session slightly earlier.