The goal of this lab is to introduce you to R and RStudio, which you’ll be using throughout the course both to learn the statistical concepts discussed in the course and to analyze real data and come to informed conclusions. To clarify which is which: R is the name of the programming language itself and RStudio is a convenient interface.
As the labs progress, you are encouraged to explore beyond what the labs dictate; a willingness to experiment will make you a much better programmer. Before we get to that stage, however, you need to build some basic fluency in R. Today we begin with the fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.
Go ahead and launch RStudio at https://rstudio.campus.pomona.edu/. You should see a window that looks like the image shown below.
The panel on the lower left is where the action happens. It is called the console. Every time you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running. Below that information is the prompt. As its name suggests, this prompt is really a request: a request for a command. Initially, interacting with R is all about typing commands and interpreting the output. These commands and their syntax have evolved over decades (literally) and now provide what many users feel is a fairly natural way to access data and organize, describe, and invoke statistical computations.
The panel in the upper right contains your workspace as well as a history of the commands that you’ve previously entered.
Any plots that you generate will show up in the panel in the lower right corner. This is also where you can browse your files, access help, manage packages, etc.
R is an open-source programming language, meaning that users can contribute packages that make our lives easier, and we can use them for free. For this lab, and many others in the future, we will use the following R packages:
dplyr
: for data wranglingggplot2
: for data visualizationinfer
: for tidy inferenceIf these packages are not already available in your R environment, install them by typing the following three lines of code into the console of your RStudio session, pressing the enter/return key after each one. Note that you can check to see which packages (and which versions) are installed by inspecting the Packages tab in the lower right panel of RStudio.
install.packages("dplyr")
install.packages("ggplot2")
install.packages("infer")
You may need to select a server from which to download; any of them will work. Next, you need to load these packages in your working environment. We do this with the library
function. Run the following three lines in your console.
library(dplyr)
library(ggplot2)
library(infer)
Note that you only need to install packages once, but you need to load (i.e., library
) them each time you relaunch RStudio.
We will be using R Markdown to create reproducible lab reports. See the following videos describing why and how:
Why use R Markdown for Lab Reports?
Using R Markdown for Lab Reports in RStudio
Going forward you should refrain from typing your code directly in the console, and instead, you should type any code (final correct answer, or anything you’re just trying out) in the R Markdown file and run the chunk using either the Run button on the chunk (green sideways triangle) or by highlighting the code and clicking Run on the top right corner of the R Markdown editor. If at any point you need to start over, you can Run All Chunks above the chunk you’re working in by clicking on the down arrow in the code chunk.
To get you started, run the following command to load the data.
data(msleep)
You can run the command by
Ctrl-Shift-Enter
, orThe data(msleep)
command instructs R to load some data: information on mammals and their sleep habits. You should see that the workspace area in the upper right-hand corner of the RStudio window now lists a data set called msleep
that has 83 observations on 12 variables. As you interact with R, you will create a series of objects. Sometimes you load them as we have done here, and sometimes you create them yourself as the byproduct of a computation or some analysis you have performed. Sometimes you will import the data from a different source.
The msleep
data set refers to sleep times and weights taken from V. M. Savage and G. B. West. A quantitative, theoretical framework for understanding mammalian sleep. Proceedings of the National Academy of Sciences, 104 (3):1051-1056, 2007.
msleep
## # A tibble: 83 x 11
## name genus vore order conservation
## <chr> <chr> <chr> <chr> <chr>
## 1 Cheetah Acinonyx carni Carnivora lc
## 2 Owl monkey Aotus omni Primates <NA>
## 3 Mountain beaver Aplodontia herbi Rodentia nt
## 4 Greater short-tailed shrew Blarina omni Soricomorpha lc
## 5 Cow Bos herbi Artiodactyla domesticated
## 6 Three-toed sloth Bradypus herbi Pilosa <NA>
## 7 Northern fur seal Callorhinus carni Carnivora vu
## 8 Vesper mouse Calomys <NA> Rodentia <NA>
## 9 Dog Canis carni Carnivora domesticated
## 10 Roe deer Capreolus herbi Artiodactyla lc
## # ... with 73 more rows, and 6 more variables: sleep_total <dbl>,
## # sleep_rem <dbl>, sleep_cycle <dbl>, awake <dbl>, brainwt <dbl>,
## # bodywt <dbl>
However printing the whole data set in the console is not particularly useful. One advantage of RStudio is that it comes with a built-in data viewer. Click on the name msleep
in the Environment pane (upper right window) that lists the objects in your workspace. This will bring up an alternative display of the data set in the Data Viewer (upper left window). You can close the data viewer by clicking on the x
in the upper left-hand corner.
What you should see are twelve columns, with each row representing a different mammal. Use the scrollbar on the right side of the console window to examine the complete data set.
Note that the row numbers in the first column are not part of the msleep
data. R adds them as part of its printout to help you make visual comparisons. You can think of them as the index that you see on the left side of a spreadsheet. In fact, the comparison to a spreadsheet will generally be helpful. R has stored the msleep
data in a kind of spreadsheet or table called a data frame.
You can see the dimensions of this data frame as well as the names of the variables and the first few observations by typing:
glimpse(msleep)
## Observations: 83
## Variables: 11
## $ name <chr> "Cheetah", "Owl monkey", "Mountain beaver", "Grea...
## $ genus <chr> "Acinonyx", "Aotus", "Aplodontia", "Blarina", "Bo...
## $ vore <chr> "carni", "omni", "herbi", "omni", "herbi", "herbi...
## $ order <chr> "Carnivora", "Primates", "Rodentia", "Soricomorph...
## $ conservation <chr> "lc", NA, "nt", "lc", "domesticated", NA, "vu", N...
## $ sleep_total <dbl> 12.1, 17.0, 14.4, 14.9, 4.0, 14.4, 8.7, 7.0, 10.1...
## $ sleep_rem <dbl> NA, 1.8, 2.4, 2.3, 0.7, 2.2, 1.4, NA, 2.9, NA, 0....
## $ sleep_cycle <dbl> NA, NA, NA, 0.1333333, 0.6666667, 0.7666667, 0.38...
## $ awake <dbl> 11.9, 7.0, 9.6, 9.1, 20.0, 9.6, 15.3, 17.0, 13.9,...
## $ brainwt <dbl> NA, 0.01550, NA, 0.00029, 0.42300, NA, NA, NA, 0....
## $ bodywt <dbl> 50.000, 0.480, 1.350, 0.019, 600.000, 3.850, 20.4...
We can see that there are 83 observations and 12 variables in the data set. The variable names are name
, genus
, vore
, etc. At this point, you might notice that many of the commands in R look a lot like functions from math class; that is, invoking R commands supplies a function with some number of arguments. The glimpse
command (that is, the glimpse
function), for example, took a single argument, the name of a data frame.
Let’s start to examine the data a little more closely. We can access the data in a single column of a data frame separately using a command like
msleep$bodywt
## [1] 50.000 0.480 1.350 0.019 600.000 3.850 20.490
## [8] 0.045 14.000 14.800 33.500 0.728 4.750 0.420
## [15] 0.060 1.000 0.005 3.500 2.950 1.700 2547.000
## [22] 0.023 521.000 187.000 0.770 10.000 0.071 3.300
## [29] 0.200 899.995 800.000 85.000 2.625 62.000 1.670
## [36] 6654.000 0.370 6.800 0.053 0.120 0.035 0.022
## [43] 0.010 0.266 1.400 0.210 0.028 2.500 55.500
## [50] 52.200 162.564 100.000 161.499 25.235 0.550 1.100
## [57] 0.021 1.620 86.000 53.180 1.100 60.000 3.600
## [64] 0.320 0.044 0.743 0.075 0.148 0.122 0.920
## [71] 0.101 0.205 0.048 86.250 4.500 0.112 207.501
## [78] 0.900 0.104 173.330 2.000 3.380 4.230
This command will only show the (average) body weight or each mammal in the data set. The dollar sign basically says “go to the data frame that comes before me, and find the variable that comes after me”.
Notice that the way R has printed these data is different from the spreadsheet View
. When we looked at the complete data frame, we saw 83 rows, one on each line of the display. The data are no longer structured in a table with other variables; they are displayed one right after another. Objects that print out in this way are called vectors; they represent a set of numbers. R has added numbers in [brackets] along the left side of the printout to indicate locations within the vector. For example, 50.0 follows [1], indicating that 50.0 is the first entry in the vector. And if [47] starts a line, then that would mean the first number on that line would represent the 47th entry in the vector.
R has some powerful functions for making graphics. We can create a simple plot of the number of girls baptized per year with the command:
qplot(x = bodywt, y = brainwt, data = msleep)
The qplot()
function (meaning “quick plot”) considers the type of data you have provided it and makes the decision to visualize it with a scatterplot. Notice that the command above again looks like a function, this time with three arguments separated by commas. The first two arguments in the qplot()
function specify the variables for the x-axis and the y-axis and the third provides the name of the data set where they can be found. We could add a fourth argument to specify the geometry that we’d like.
qplot(x = vore, y = sleep_total, data = msleep, geom = "boxplot")
You might wonder how you are supposed to know that it was possible to add that fourth argument. Thankfully, R documents all of its functions extensively. To read what a function does and learn the arguments that are available to you, just type in a question mark followed by the name of the function that you’re interested in. Try the following.
?qplot
?msleep
Notice that the help show up in the lower right panel of your screen. You can toggle between plots, files, packages, and help files using the tabs at the top of that panel.
log="xy"
to log transform both axes. (To ensure that your lab report is comprehensive, be sure to include the code needed to make the plot as well as your written interpretation.)Now, suppose we want to figure out the amount of non-rem sleep each animal gets. For the Owl monkey, it is 17.0 - 1.8. We can use R as a calculator.
17 - 1.8
## [1] 15.2
to see the total amount of non-rem sleep is 15.2 hours. We could repeat this once for each animal, but there is a faster way. If we subtract the vector for rem sleep from the total sleep, R will compute all differences simultaneously.
msleep$sleep_total - msleep$sleep_rem
## [1] NA 15.2 12.0 12.6 3.3 12.2 7.3 NA 7.2 NA 4.7 8.6 9.3 11.0
## [15] 8.1 6.3 7.7 14.3 4.8 13.1 NA 15.8 2.3 2.7 6.6 9.8 NA 9.3
## [29] 8.7 1.5 2.6 4.7 5.7 6.1 8.6 NA 12.8 8.9 12.3 11.2 NA 11.1
## [43] 17.9 NA NA 6.8 NA 7.5 3.2 8.3 NA NA NA 8.4 7.6 NA
## [57] NA 11.9 3.1 NA 9.6 12.0 4.9 10.6 NA 8.2 6.3 10.2 8.2 NA
## [71] 10.4 12.9 10.8 6.7 NA NA 3.4 13.3 6.3 NA 5.0 NA 7.4
What you will see are 83 numbers, each one representing the difference we’re after. Take a look at a few of them and verify that they are right. Note also the large number of missing values. Why are those there? How might you want to deal with the NA values?
We’ll be using the new sleep vector to generate some plots, so we’ll want to save it as a permanent column in our data frame.
msleep <- msleep %>%
mutate(allrem = ifelse(is.na(sleep_rem), 0, sleep_rem)) %>%
mutate(nonrem = sleep_total - allrem)
The %>%
operator is called the piping operator. It takes the output of the previous expression and pipes it into the first argument of the function in the following one. To continue our analogy with mathematical functions, x %>% f(y)
is equivalent to f(x, y)
. Piping can be read in English as “and then”.
A note on piping: Note that we can read these three lines of code as the following:
“Take the msleep
dataset and pipe it into the mutate
function. Mutate the msleep
data set by creating a new variable called allrem
which is zero if the rem column is missing. Then create a column called nonrem
which is the difference of the variables sleep_total
and allrem
. Then assign the resulting data set to the object called msleep
, i.e. overwrite the old msleep
data set with the new one containing the new variable.”
Where is the new variable? When you make changes to variables in your data set, click on the name of the data set again to update it in the data viewer.
You’ll see that there are now two new columns called allrem
and nonrem
that have been tacked on to the data frame. The special symbol <-
performs an assignment, taking the output of one line of code and saving it into an object in your workspace. In this case, you already have an object called msleep
, so this command updates that data set with the new mutated column.
We can make a plot of the total number of baptisms per year with the command
qplot(x = nonrem, facets = ~vore, data = msleep, geom = "histogram")
Tip: If you use the up and down arrow keys, you can scroll through your previous commands, your so-called command history. You can also access it by clicking on the history tab in the upper right panel. This will save you a lot of typing in the future.
Finally, in addition to simple mathematical operators like subtraction and division, you can ask R to make comparisons like greater than, >
, less than, <
, and equality, ==
. For example, we can ask if nonrem or rem represent more hours of sleep.
msleep <- msleep %>%
mutate(more_rem = sleep_rem > nonrem)
The mutate command adds a new variable to the msleep
data frame containing the values of either TRUE
if that mammal has more rem sleep, or FALSE
if that mammal has more non-rem sleep. This variable contains a different kind of data than we have encountered so far. All other columns in the msleep
data frame have values that are numerical or categorical (the year, the number of boys and girls). Here, we’ve asked R to create logical data, data where the values are either TRUE
or FALSE
. In general, data analysis will involve many different kinds of data types, and one reason for using R is that it is able to represent and compute with many of them.
With all of the new variables you’ve created, you might want to look at your data frame one more time.
glimpse(msleep)
## Observations: 83
## Variables: 14
## $ name <chr> "Cheetah", "Owl monkey", "Mountain beaver", "Grea...
## $ genus <chr> "Acinonyx", "Aotus", "Aplodontia", "Blarina", "Bo...
## $ vore <chr> "carni", "omni", "herbi", "omni", "herbi", "herbi...
## $ order <chr> "Carnivora", "Primates", "Rodentia", "Soricomorph...
## $ conservation <chr> "lc", NA, "nt", "lc", "domesticated", NA, "vu", N...
## $ sleep_total <dbl> 12.1, 17.0, 14.4, 14.9, 4.0, 14.4, 8.7, 7.0, 10.1...
## $ sleep_rem <dbl> NA, 1.8, 2.4, 2.3, 0.7, 2.2, 1.4, NA, 2.9, NA, 0....
## $ sleep_cycle <dbl> NA, NA, NA, 0.1333333, 0.6666667, 0.7666667, 0.38...
## $ awake <dbl> 11.9, 7.0, 9.6, 9.1, 20.0, 9.6, 15.3, 17.0, 13.9,...
## $ brainwt <dbl> NA, 0.01550, NA, 0.00029, 0.42300, NA, NA, NA, 0....
## $ bodywt <dbl> 50.000, 0.480, 1.350, 0.019, 600.000, 3.850, 20.4...
## $ allrem <dbl> 0.0, 1.8, 2.4, 2.3, 0.7, 2.2, 1.4, 0.0, 2.9, 0.0,...
## $ nonrem <dbl> 12.1, 15.2, 12.0, 12.6, 3.3, 12.2, 7.3, 7.0, 7.2,...
## $ more_rem <lgl> NA, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, NA,...
We will be using R Markdown to create reproducible lab reports and homework assignments. See the following video describing how to get started with creating reports for this lab, and all future labs:
Basic R Markdown with an OpenIntro Lab
In the previous few pages, you worked with the mammal sleep data to get to know R. Your lab assignment involves continuing to work with the data to answer a few new questions.
Which mammal sleeps the most in total (on average) and how much do they sleep? What about the least? [Hint: use the data Viewer and sort the variables.]
Repeat the previous problem using piping. To include the sorted result in your report you will need to use two new functions: arrange
(for sorting). We can arrange the data in a descending order with another function: desc
(for descending order). The default for arrange is to sort in ascending order. Sample code provided below.
msleep %>%
select(name, bodywt) %>%
arrange(desc(bodywt))
## # A tibble: 83 x 2
## name bodywt
## <chr> <dbl>
## 1 African elephant 6654.000
## 2 Asian elephant 2547.000
## 3 Giraffe 899.995
## 4 Pilot whale 800.000
## 5 Cow 600.000
## 6 Horse 521.000
## 7 Brazilian tapir 207.501
## 8 Donkey 187.000
## 9 Bottle-nosed dolphin 173.330
## 10 Tiger 162.564
## # ... with 73 more rows
This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel from a lab written by Mark Hansen of UCLA Statistics.
That was a short introduction to R and RStudio, but we will provide you with more functions and a more complete sense of the language as the course progresses.
In this course we will be using R packages called dplyr
for data wrangling and ggplot2
for data visualization. If you are googling for R code, make sure to also include these package names in your search query. For example, instead of googling “scatterplot in R”, google “scatterplot in R with ggplot2”.
The following cheatsheets may come in handy throughout the semester:
Chester Ismay has put together a resource for new users of R, RStudio, and R Markdown here. It includes examples showing working with R Markdown files in RStudio recorded as GIFs.
Note that some of the code on these cheatsheets may be too advanced for this course, however the majority of the information will become useful throughout the semester.
HW & Lab assignments will be graded out of 5 points, which are based on a combination of accuracy and effort. Below are rough guidelines for grading.
5 points: All problems completed with detailed solutions provided and 75% or more of the problems are fully correct.
4 points: All problems completed with detailed solutions and 50-75% correct; OR close to all problems completed and 75%-100% correct. An assignment will earn a 4 if there is superfluous information printed out on the assignment.
3 points: Close to all problems completed with less than 75% correct
2 points: More than half but fewer than all problems completed and > 75% correct
1 point: More than half but fewer than all problems completed and < 75% correct; OR less than half of problems completed
0 points: No work submitted, OR half or less than half of the problems submitted and without any detail/work shown to explain the solutions.