An Introduction to R for Quantitative Methods
2023-04-05
PREFACE
This book provides an introduction to R programming and serves as a companion to the data visualization workshop at NCME. It pulls from many sources, including An Introduction to R for Quantitative Methods, R for Data Science, What They Forgot to Teach You About R, Data Visualization: A Practical Introduction, Tidy Modeling with R, and Applied Statistical Computing.
Conventions1
In this book, we alternate between regular text (like this), samples of code that you can type and run yourself, and the output of that code. In the main text, references to objects or other things that exist in the R language or in your R project—like tables of data, variables, functions, and so on—will also appear in a monospaced
or "typewriter" typeface
. Code you can type directly into R at the console will be in gray boxes, and also monospaced. Like this:
my_numbers <- c(1, 1, 4, 1, 1, 4, 1)
If you type that line of code into R’s console it will create a thing called my_numbers
. Doing this doesn’t produce any output, however. When we write code that also produces output at the console, we will first see the code (in a gray box) and then the output in a monospaced font against a gray background. Here we add two numbers and see the result:
4 + 1
## [1] 5
Two further notes about how to read this. First, by default in this book, anything that comes back to us at the console as the result of typing a command will be shown prefaced by two hash characters (##
) at the beginning of each line of output. This is to help distinguish it from commands we type into the console. You will not see the hash characters at the console when you use R.
Second, both in the book and at the console, if the output of what you did results in a series of elements (like numbers, or observations from a variable, and so on) you will often see output that includes some number in square brackets at the beginning of the line. It looks like this: [1]
. This is not part of the output itself, but just a counter or index keeping track of how many items have been printed out so far. In the case of adding 4 + 1
we got just one, or [1]
, thing back—the number five. If there are more elements returned as the result of some instruction or command, the counter will keep track of that on each line. In this next bit of code we will tell R to show us the lower-case letters of the alphabet:
letters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
You can see the counter incrementing on each line as it keeps count of how many letters have been printed.
This section is repurposed from Data Visualization: A Practical Introduction↩︎