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
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
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,
Values can be compared to one another. The result is always a vector of
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(1, 4, 2, 8, 0, 10) %in% c(1, 2, 3) #>  TRUE FALSE TRUE FALSE FALSE FALSE # Are these states a part of New England? states <- c("VA", "CT", "MA", "SD", "GA", "AL", "ND", "SD", "VT") new_england <- c("MA", "ME", "CT", "RI", "NH", "VT") states %in% new_england #>  FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
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 has no value. But by using the
na.rm argument to
sum(), we can instruct it to ignore
R has many built-in functions. For instance, the
sort() function can sort many different kinds of values.
x #>  1 5 2 4 2 5 sort(x) #>  1 2 2 4 5 5 sort(x, decreasing = TRUE) #>  5 5 4 2 2 1 states #>  "VA" "CT" "MA" "SD" "GA" "AL" "ND" "SD" "VT" sort(states) #>  "AL" "CT" "GA" "MA" "ND" "SD" "SD" "VA" "VT" sort(states, decreasing = TRUE) #>  "VT" "VA" "SD" "SD" "ND" "MA" "GA" "CT" "AL"
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
}, 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.
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
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).
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 (
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
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
library(historydata) library(tidyverse) us_state_populations #> # A tibble: 983 x 4 #> GISJOIN year state population #> <chr> <int> <chr> <int> #> 1 G090 1790 Connecticut 237655 #> 2 G100 1790 Delaware 59096 #> 3 G130 1790 Georgia 82548 #> 4 G240 1790 Maryland 319728 #> 5 G250 1790 Massachusetts 475199 #> 6 G330 1790 New Hampshire 141899 #> # ... with 977 more rows
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.
us_state_populations[5:8, ] # rows 5-8, all columns #> # A tibble: 4 x 4 #> GISJOIN year state population #> <chr> <int> <chr> <int> #> 1 G250 1790 Massachusetts 475199 #> 2 G330 1790 New Hampshire 141899 #> 3 G340 1790 New Jersey 184139 #> 4 G360 1790 New York 340241 us_state_populations[ , 2:3] # all rows, columns 2-3 #> # A tibble: 983 x 2 #> year state #> <int> <chr> #> 1 1790 Connecticut #> 2 1790 Delaware #> 3 1790 Georgia #> 4 1790 Maryland #> 5 1790 Massachusetts #> 6 1790 New Hampshire #> # ... with 977 more rows us_state_populations[5:8, 2:3] # rows 5-8, colums 2-3 #> # A tibble: 4 x 2 #> year state #> <int> <chr> #> 1 1790 Massachusetts #> 2 1790 New Hampshire #> 3 1790 New Jersey #> 4 1790 New York
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.
us_state_populations[us_state_populations$state == "Massachusetts", ] #> # A tibble: 23 x 4 #> GISJOIN year state population #> <chr> <int> <chr> <int> #> 1 G250 1790 Massachusetts 475199 #> 2 G250 1800 Massachusetts 574564 #> 3 G250 1810 Massachusetts 700745 #> 4 G250 1820 Massachusetts 523287 #> 5 G250 1830 Massachusetts 610408 #> 6 G250 1840 Massachusetts 737699 #> # ... with 17 more rows
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.
m <- matrix(c(NA, NA, NA, NA, NA, 0.31, NA, NA, NA, NA, 0.29, 0.95, NA, NA, NA, 0.66, 0.7, 0.18, NA, NA, 0.41, 0.3, 0.49, 0.7, NA), nrow = 5, ncol = 5) rownames(m) <- letters[1:5] colnames(m) <- letters[1:5] m #> a b c d e #> a NA 0.31 0.29 0.66 0.41 #> b NA NA 0.95 0.70 0.30 #> c NA NA NA 0.18 0.49 #> d NA NA NA NA 0.70 #> e NA NA NA NA NA
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
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
Wickham, Advanced R, chs. 2–3.↩
I’m calling certain parts of R syntax operators, but technically they are special functions with syntactic sugar.↩
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
[[operators so that they do not use the argument
drop = TRUEby default. See that package’s documentation for more detail.↩