Chapter 1 R Basics

This chapter covers the basics: installing and using packages and loading data.

Most of the recipes in this book require the ggplot2, dplyr, and gcookbook packages to be installed on your computer. (The gcookbook package contains data sets used in some of the examples, but is not necessary for doing your real work.) If you want to get started quickly, run:

Then, in each R session, before running the examples in this book, you can load them with:

Running library(tidyverse) will load ggplot2, dplyr, and a number of other packages. If you want to keep your R session more streamlined and load only the packages that are strictly needed, you can load ggplot2 and dplyr packages individually:

Note

If you want a deeper understanding of how ggplot2 works, see Appendix A, which explains the concepts behind ggplot2.

Packages in R are collections of functions and/or data that are bundled up for easy distribution, and installing a package will extend the functionality of R on your computer. If an R user creates a package and thinks that it might be useful for others, that user can distribute it through a package repository. The primary repository for distributing R packages is called CRAN (the Comprehensive R Archive Network), but there are others, such as Bioconductor, which specializes in packages related to genomic data.

If you have spent much time learning R, you may have heard of the tidyverse, which is a collection of R packages that share common ideas of how data should be structured and manipulated. This is in contrast to base R, which is the set of packages that are included when you just download and install R. The tidyverse is a set of add-ons for R, which make it easier to do many operations related to data manipulation and visualization. This book mostly uses the tidyverse, as I believe that it provides a quicker and simpler (but not less powerful!) way to work with data.

If you haven’t used the tidyverse before, there is one recipe in particular that you should read that will help you understand a foreign-looking bit of syntax: %>%, also known as the pipe operator. This is Recipe 1.7 in this chapter.

1.1 Installing a Package

1.1.1 Problem

You want to install a package from CRAN.

1.1.2 Solution

Use install.packages() and give it the name of the package you want to install. To install ggplot2, run:

At this point you may be prompted to select a download mirror. It’s usually best to use the first choice, https://cloud.r-project.org/, as it is a cloud-based mirror with endpoints all over the world.

1.1.3 Discussion

If you want to install multiple packages at once, you can pass it a vector of package names. For example, this will install most of the packages used in this book:

When you tell R to install a package, it will automatically install any other packages that the first package depends on.

CRAN (the Comprehensive R Archive Network) is a repository of packages for R, and it is mirrored on many servers around the world. It is the default repository system used by R. There are other package repositories; Bioconductor, for example, is a repository of packages related to analyzing genomic data.

1.2 Loading a Package

1.2.1 Problem

You want to load an installed package.

1.2.2 Solution

Use library() and give it the name of the package you want to install. To load ggplot2, run:

The package must already be installed on the computer.

1.2.3 Discussion

Most of the recipes in this book require loading a package before running the code, either for the graphing capabilities (as in the ggplot2 package) or for example data sets (as in the MASS and gcookbook packages).

One of R’s quirks is the package/library terminology. Although you use the library() function to load a package, a package is not a library, and some longtime R users will get irate if you call it that.

A library is a directory that contains a set of packages. You might, for example, have a system-wide library as well as a library for each user.

1.3 Upgrading Packages

1.3.1 Problem

You want to upgrade a package that is already installed.

1.3.2 Solution

Run update.packages():

It will prompt you for each package that can be upgraded. If you want it to upgrade all packages without asking, use ask = FALSE:

1.3.3 Discussion

Over time, package authors will release new versions of packages with bug fixes and new features, and it’s usually a good idea to keep up-to-date. However, keep in mind that occasionally new versions of packages will introduce bugs or have slightly changed behavior.

1.4 Loading a Delimited Text Data File

1.4.1 Problem

You want to load data from a delimited text file.

1.4.2 Solution

The most common way to read in a file is to use comma-separated values (CSV) data:

Alternatively, you can use the read_csv() function (note the underscore instead of period) from the readr package. This function is significantly faster than read.csv(), and

1.4.3 Discussion

Since data files have many different formats, there are many options for loading them. For example, if the data file does not have headers in the first row:

The resulting data frame will have columns named V1, V2, and so on, and you will probably want to rename them manually:

You can set the delimiter with sep. If it is space-delimited, use sep = " ". If it is tab-delimited, use \t, as in:

By default, strings in the data are treated as factors. Suppose this is your data file, and you read it in using read.csv():

"First","Last","Sex","Number"
"Currer","Bell","F",2
"Dr.","Seuss","M",49
"","Student",NA,21

The resulting data frame will store First and Last as factors, though it makes more sense in this case to treat them as strings (or character vectors in R terminology). To differentiate this, use stringsAsFactors = FALSE. If there are any columns that should be treated as factors, you can then convert them individually:

Alternatively, you could load the file with strings as factors, and then convert individual columns from factors to characters.

1.4.4 See Also

read.csv() is a convenience wrapper function around read.table(). If you need more control over the input, see ?read.table.

1.5 Loading Data from an Excel File

1.5.1 Problem

You want to load data from an Excel file.

1.5.2 Solution

The readxl package has the function read_excel() for reading .xls and .xlsx files from Excel. This will read the first sheet of an Excel spreadsheet:

1.5.3 Discussion

With read_excel(), you can load from other sheets by specifying a number for sheetIndex or a name for sheetName:

read_excel() uses the first row of the spreadsheet for column names. If you don’t want to use that row for column names, use col_names = FALSE. The columns will instead be named X1, X2, and so on.

By default, read_excel() will infer the type of each column, but if you want to specify the type of each column, you can use the col_types argument. You can also drop columns if you specify the type as "blank".

1.5.4 See Also

See ?read_excel for more options controlling the reading of these files.

There are other packages for reading Excel files. The gdata package has a function read.xls() for reading in .xls files, and the xlsx package has a function read.xlsx() for reading in .xlsx files. They require external software to be installed on your computer: read.xls() requires Java, and read.xlsx() requires Perl.

1.6 Loading Data from SPSS/SAS/Stata Files

1.6.1 Problem

You want to load data from a SPSS file, or from other programs like SAS or Stata.

1.6.2 Solution

The haven package has the function read_sav() for reading SPSS files. To load data from an SPSS file:

1.6.3 Discussion

The haven package also includes functions to read from other formats:

  • read_sas(): SAS
  • read_dta(): Stata

An alternative to haven is the foreign package. It also supports SPSS and Stata files, but it is not as up-to-date as the functions from haven. For example, it only supports Stata files up to version 12, while haven supports up to version 14 (the current version as of this writing).

The foreign package does support some other formats, including:

  • read.octave(): Octave and MATLAB
  • read.systat():SYSTAT
  • read.xport(): SAS XPORT
  • read.dta(): Stata
  • read.spss(): SPSS

1.6.4 See Also

Run ls("package:foreign") for a full list of functions in the foreign package.

1.7 Chaining Functions Together With %>%, the Pipe Operator

1.7.1 Problem

You want to call one function, then pass the result to another function, and another, in a way that is easily readable.

1.7.2 Solution

Use %>%, the pipe operator. For example:

This takes the morley data set, passes it to the filter() function from dplyr, keeping only the rows of the data where Expt is equal to 1. Then that result is passed to the summary() function, which calculates some summary statistics on the data.

Without the pipe operator, the code above would be written like this:

In this code, function calls are processed from the inside outward. From a mathematical viewpoint, this makes perfect sense, but from a readability viewpoint, this can be confusing and hard to read, especially when there are many nested function calls.

1.7.3 Discussion

This pattern, with the %>% operator, is widely used with tidyverse packages, because they contain many functions that do relatively small things. The idea is that these functions are building blocks that allow user to compose the function calls together to produce the desired result.

To illustrate what’s going on, here’s a simpler example of two equivalent pieces of code:

The pipe operator in essence takes the thing that’s on the left, and places it as the first argument of the function call that’s on the right.

It can be used for multiple function calls, in a chain:

In a function chain, the lexical ordering of the function calls is the same as the order in which they’re computed.

If you want to store the final result, you can use the <- operator at the beginning. For example, this will replace the original x with the result of the function chain:

If there are additional arguments for the function calls, they will be shifted to the right when the pipe operator is used. Going back to code from the first example, these two are equivalent:

The pipe operator is actually from the magrittr package, but dplyr imports it and makes it available when you call library(dplyr)

1.7.4 See Also

For many more examples of how to use %>% in data manipulation, see Chapter 15.