1 Getting Started

R is an open source programming language for statistical computing and graphics that is widely used among statisticians for data analysis. As R is a programming language and not a program itself (like Excel) we will need some programming skills as even the simplest data analysis will require writing and evaluating small fragments of code to carry out the analysis we require. To make life easier, we will be using RStudio as to write, edit, and evaluate our R code.

Both R and RStudio are available on the University network. You will find R Studio on the AppHub on your Desktop.

1.1 RStudio

The default RStudio environment is divided in 4 panels, with the default arrangement illustrated below. Going anticlockwise, the four panels are: Code; Console; Plots, Help, and Files; and Workspace and History.

  • Code Editor – the main editor for your R code. Your code will not be evaluated until you “Run” it. This pane is hidden at first, however when you load or start a new R script file it will be displayed. R code can be entered here, and it provides support such as auto-complete using [Tab], colour highlighting, and additional buttons and menu items to help edit and evaluate your code. In particular, a single line of code (or any selected block of code) can be evaluated in one go by typing [Ctrl]+[Enter], and the whole file can be evaluated with [Ctrl]+[Shift]+[S].

  • R Console – this pane is where your code from the Editor is evaluated by R. You can also use the console here to execute quick calculations that you don’t need to save. Commands entered in the Console tab are immediately executed by R, and the results displayed on the following line. In this way, R can be used as a simple calculator by typing directly into the Console window. However, for more complex calculations with many steps it is preferable to write the code in a script file using the Code pane first, and then evaluate it in the Console. Note: when in the Console pane, you can use the (up arrow) and (down arrow) keys to navigate through previous commands (e.g. to correct mistakes).

  • Plots, Help, and Files – this pane has multiple roles indicated by the tabs along its top. The Plots tab will show the results of any plots you produce in R. The Help tab is where RStudio will display any help files. The Files tab provides a simple file viewer to quickly navigate between and open files.

  • Workspace and History – this pane has two functions, also indicated by tabs. The Workspace (or Environment) tab lists all the variables you have currently available in this session of R, along with their types and values. The History tab shows a list of all the R commands you have evaluated in the console.

For more detailed help on the RStudio programming environment, see the RStudio cheatsheet.

1.2 Packages and libraries

R packages are collections of functions and data sets developed by the community. They increase the power of R by improving existing base R functionalities, or by adding new ones.

Most of the time we’ll be happy with the core functionality of R but sometimes we wil need to load packages to access specific data sets or functions that are not provided by default.

1.2.1 Load a package with library

A package called packagename is loaded using the following command:

library("packagename")

In particular, we will often need the durham package as it contains many of the data sets we will be working with. We load this package as follows

library("durham")

R Help: library

1.2.2 Load a data set with data

To use a data set provided by a package, we must first load a package using the library function and then we must load the data using the data function. For example, to load the cats data set from the MASS (Modern Applied Statistics) package, we first load the package and then call data to load the data set:

library("MASS")
data("cats")

R Help: data

1.3 Help

1.3.1 Getting help on a function

To view the R help file for a particular function, we can either use the help function or the question mark ?. For example, for help with the mean function both of the following commands will display the same help file in the Help pane

help(mean)
?mean

R Help: help

1.3.2 Getting help on a package

To view the entire collection of help files for a particular package, we again use help but this time must explicitly state that we’re interested in a package. So, for help with the MASS package we must write

help(package="MASS")