C An R primer

What follows is a highly condensed guide to the basic features of the R programming language and some of its most useful general purpose packages. Use this primer as a quick introduction to the language, or as a reference when reading the rest of the book. You should also read at a minimum the chapters on “Data Structures” and “Subsetting” from Advanced R, since understanding those concepts are crucial to working effectively in R.28

C.1 Values

A value is the most basic piece of data. There are several kinds of values.

Doubles are positive or negative numbers that can have a decimal point. Integers are positive or negative whole numbers. Even if a value does not have a decimal point, it is a double unless it is suffixed with L.

Text is stored in character vectors (usually called strings in other programming langauges). Characters are marked by surrounding single or double quotation marks.

Logical values, or booleans, can be either true or false.

Factors are a special kind of text value, where the values must be part of a predefined set of options. Factors are usually used for categorical data.

Each of these kinds of values has a corresponding missing value, NA. These are used for missing or unrecorded data.

C.2 Variables and assignment

Values can be stored in variables. A value is placed into a variable using the assignment operator, <-.29

C.3 Comparison

Values can be compared to one another. The result is always a vector of TRUE and FALSE values indicating whether the condition was met or not.

Comparison also works with character vectors.

Notice that equality is tested with the == operater, not with =.

Often it is helpful to test whether elements of one vector are contained in another vector with the %in% function. Notice that the resulting vector is the same length as the vector on the left-hand side.

C.4 Functions

R is a functional programming language, so knowing how to apply functions to problems, and to write them yourself, is essential to doing your work. Functions are the verbs of R programming.

C.4.1 Using functions

Functions can be called on many different kinds of R vectors and objects. A function takes an input and produces an output. In this case we will use sum() to add up a vector of numbers.

Notice that calls to functions are always followed by parentheses, containing the function arguments. In the example above, x was the only argument to the function; it was the means by which we passed data into the function. Functions can often take many arguments that specify options to function. For instance, if we have a vector of numbers that contains an missing value (NA), then the function sum() will return NA, because NA has no value. But by using the na.rm argument to sum(), we can instruct it to ignore NA values.

R has many built-in functions. For instance, the sort() function can sort many different kinds of values.

It is important to note that most functions you use to work with data are pure functions. The results of pure functions depend only on their inputs: the same input will always produce the same output. And in general, R functions do not modify their inputs directly, but instead return a copy they modify their inputs. Notice that in this example the original states vector remains unsorted, even after it has been passed through the sort() function, and a new copy of the data, now sorted, is stored in the variable states_sorted. This makes it easier to reason about what a function will do.

C.4.2 Writing your own functions

You will often have to write your own functions. You can think of a function as encapsulating some action that you take on your data. Consider the following few sentences, each of which includes a year. How can we turn the year in each of these sentences into a number?

We can write a function that does this work for us. The function will be named extract_year. It will have a single argument, sent, which will be a sentence containing a year. The body of the function, between { and }, does the work of finding the 4-character string of digits then turning that into an integer. The last value in the function will be returned as its output.

Now we can call the function on our data and get the desired result:

C.5 Data structures and subsetting

If functions are the verbs of R programming, data are the nouns. And data can be stored in many different kinds of data structures, including vectors, lists, data frames, and matrices. These kinds of data structures share a set of operators for subsetting them, that is, for pulling out pieces of the data.

C.5.1 Vectors

The most basic data structure is a vector. Consider this vector of 10 names.

In R, elements of a vector are numbered, starting from 1 to the length of the vector. (Most programming languages have a 0-based indexing.) We can get individual elements of the vector using a numeric vector and the [ subsetting operator. For example, we can get the fifth element, or the first, eighth, tenth, and sixth elements together.

We can also subset a vector by using a logical vector. When we use a logical vector, we get back the elements that correspond to the TRUE values. It is usually a good idea to use a logical vector which has same length as the original vector. In this example, we use a function to test whether the names in our people vector have a lowercase e in them. Then we use that logical vector and the [ subset operator to return only the names that have that letter in them.

It is possible for a vector to have names, which we can also use for subsetting. Suppose we have a set of numbers from 1 to 10 that correspond to a rank for each person. At first our vector does not have any names.

But we can use our people vector to give it names.

Now we can get the fifth rank and see the corresponding name.

We can use a character vector to subset rank to using those names.

And we can sort rank and see that the names are also sorted, or get only the elements of rank which are above a certain value (another example of logical subsetting).

C.5.2 Lists

Lists are conceptually an extension of vectors. They are a vector which can contain other vectors, each of which can contain a different kind of information. Lists can thus have an arbitrary structure. Let’s create a list that models a historical event, recording certain key pieces of information. Notice that some of the information is numeric and some is text; some of the fields have a single entry, and some are a vector of values.

Because lists can have an arbitrary structure, you can use the str() function to see what a list contains. (This function works on any R object.) Notice that this is a list that has five elements, and that each of those elements has a name (date, price, and so on).

Just like a regular vector, you can subset a list with a numerical index.

But notice that the resulting value is a list containing the date. We have selected an element of the list, but that element still comes wrapped in the list. We can get what is contained inside each element by using the double bracket subset operator, [[. Notice that the value returned is now the number that corresponds to the date.

Just like with regular vectors, you can also subset a list by its names if it has them.

The single bracket subset operator [ gives us a list, but we can get a named element itself with the double bracked subset operator [[.

There is a special syntax for accessing a named element of a list using the $ operator. This is the equivalent of event[["date"]].

C.5.3 Data frames

A data frame is like a spreadsheet, since it is a table with columns and rows. It is the data structure that we will use most in this book, though we will mostly use the dplyr package instead of base R to manipulate tabular data. Technically a data frame is a list where the constituent vectors are all the same length, and so you can use the same subset operators on data frames. Even if you mostly use dplyr, it is still important to understand how data frames work in base R.30

We can load a data frame of historical state populations from the historydata package. In an interactive session, you can get a nice view of the data frame by running View(us_state_populations).

You can get the vector for any column by using the column name with $.

You can also subset a data frame using [. But because data frames are two dimensional, you should specify a subset index for both rows and columns. Leave either one blank to specify all columns or all rows. In the examples below there are extra spaces to make it clear where we are subsetting by rows and where by columns.

You can subset a data frame using a logical operator. Here we use a common idiom to find create a logical vector where we check whether each row in the state column is equal to "Massachusetts", and then use that vector to subset the data frame’s rows. (Notice the space after the ,.) The result is that we have filtered the data frame to just the Massachusetts population. This is much less verbose in dplyr, but there are times when you will need to manipulate a data frame in base R.

C.5.4 Matrices

A matrix is a very common data structure in computational history, useful for holding counts of words in documents, similarities between texts, distances between points, and the like. A matrix is a vector where all of the elements are of the same type, but it is arranged in two dimensions. Consider this matrix, which could represent how similar documents are to one another.

Notice that we only need the upper triangle of the matrix because presumably the similarities of two documents are the same in either direction, and the similarity of a document to itself is not a very meaningful question. You can subset a matrix with [ by rows and columns. Here we get the similarity of a to c, and then the similarity of a to every text.

Notice that the [ operator simplifies the results to a one-dimensional vector or single number if possible, rather than giving us a matrix back. This is sometimes undesirable, and we can avoid it by using the drop = FALSE argument to [.

  1. Wickham, Advanced R, chs. 2–3.

  2. I’m calling certain parts of R syntax operators, but technically they are special functions with syntactic sugar.

  3. The tibble package, part of the tidyverse, changes a few things about how data frames function in R. It makes them print in a more friendly way, but it also changes the [ and [[ operators so that they do not use the argument drop = TRUE by default. See that package’s documentation for more detail.