The RStudio Interface

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. You should see a window that looks like the image shown below.


The panel on the lower left is where the action happens. It’s called the console. Everytime 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.

Loading This Lab Into RStudio

There are 5 steps to getting this lab loaded into R Studio:

  1. Download the .Rmd file from the course website, and save it on your laptop in a place where you’ll be able to find it.

  2. In RStudio, go to the Files panel in the lower right (you may already be there).

  3. Click the “New Folder” button to make a folder for this lab. You could call it “lab1”. Then click on the folder name to go into it.

  4. Click the “Upload” button to upload the .Rmd file you downloaded in step 1 from your laptop to the RStudio server.

  5. Click on the file in the RStudio to open it.

R Packages

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 wrangling
  • ggplot2: for data visualization
  • oilabs: for data

In order to use the functions and data provided by these packages, you need to load them in your working environment. We do this with the library function. Run the following three lines:

library(dplyr)
library(ggplot2)
library(oilabs)

You can do this by

  • putting your cursor on the line and clicking the Run button on the upper right corner of the pane, or
  • typing the code in the console.

From now on, it’s best to use the Run button to run code that is entered in an R chunk in the R Markdown document. That way, you’ll always have a record of everything you’ve done.

Note that dplyr and ggplot2 are part of a collection of R packages called the tidyverse. These packages share common philosophies and are designed to work together. You can find more about the packages in the tidyverse at http://tidyverse.org/ (but this is not critical reading for today’s lab).

Dr. Arbuthnot’s Baptism Records

To get you started, run the following command to load the data.

data(arbuthnot)

This command instructs R to load some data: the Arbuthnot baptism counts for boys and girls (note that all babies were classified as either a boy or a girl – this is a limitation of the data). You should see that the workspace area in the upper righthand corner of the RStudio window now lists a data set called arbuthnot that has 82 observations on 3 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.

The Arbuthnot data set refers to Dr. John Arbuthnot, an 18th century physician, writer, and mathematician. He was interested in the ratio of newborn boys to newborn girls, so he gathered the baptism records for children born in London for every year from 1629 to 1710. We can view the data by typing its name into the console.

arbuthnot
##    year boys girls
## 1  1629 5218  4683
## 2  1630 4858  4457
## 3  1631 4422  4102
## 4  1632 4994  4590
## 5  1633 5158  4839
## 6  1634 5035  4820
## 7  1635 5106  4928
## 8  1636 4917  4605
## 9  1637 4703  4457
## 10 1638 5359  4952
## 11 1639 5366  4784
## 12 1640 5518  5332
## 13 1641 5470  5200
## 14 1642 5460  4910
## 15 1643 4793  4617
## 16 1644 4107  3997
## 17 1645 4047  3919
## 18 1646 3768  3395
## 19 1647 3796  3536
## 20 1648 3363  3181
## 21 1649 3079  2746
## 22 1650 2890  2722
## 23 1651 3231  2840
## 24 1652 3220  2908
## 25 1653 3196  2959
## 26 1654 3441  3179
## 27 1655 3655  3349
## 28 1656 3668  3382
## 29 1657 3396  3289
## 30 1658 3157  3013
## 31 1659 3209  2781
## 32 1660 3724  3247
## 33 1661 4748  4107
## 34 1662 5216  4803
## 35 1663 5411  4881
## 36 1664 6041  5681
## 37 1665 5114  4858
## 38 1666 4678  4319
## 39 1667 5616  5322
## 40 1668 6073  5560
## 41 1669 6506  5829
## 42 1670 6278  5719
## 43 1671 6449  6061
## 44 1672 6443  6120
## 45 1673 6073  5822
## 46 1674 6113  5738
## 47 1675 6058  5717
## 48 1676 6552  5847
## 49 1677 6423  6203
## 50 1678 6568  6033
## 51 1679 6247  6041
## 52 1680 6548  6299
## 53 1681 6822  6533
## 54 1682 6909  6744
## 55 1683 7577  7158
## 56 1684 7575  7127
## 57 1685 7484  7246
## 58 1686 7575  7119
## 59 1687 7737  7214
## 60 1688 7487  7101
## 61 1689 7604  7167
## 62 1690 7909  7302
## 63 1691 7662  7392
## 64 1692 7602  7316
## 65 1693 7676  7483
## 66 1694 6985  6647
## 67 1695 7263  6713
## 68 1696 7632  7229
## 69 1697 8062  7767
## 70 1698 8426  7626
## 71 1699 7911  7452
## 72 1700 7578  7061
## 73 1701 8102  7514
## 74 1702 8031  7656
## 75 1703 7765  7683
## 76 1704 6113  5738
## 77 1705 8366  7779
## 78 1706 7952  7417
## 79 1707 8379  7687
## 80 1708 8239  7623
## 81 1709 7840  7380
## 82 1710 7640  7288

However printing the whole dataset in the console is not that useful. One advantage of RStudio is that it comes with a built-in data viewer. Click on the name arbuthnot 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 lefthand corner.

What you should see are four columns of numbers, each row representing a different year: the first entry in each row is simply the row number (an index we can use to access the data from individual years if we want), the second is the year, and the third and fourth are the numbers of boys and girls baptized that year, respectively. 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 Arbuthnot’s 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 Arbuthnot’s 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 running the following command:

str(arbuthnot)
## 'data.frame':    82 obs. of  3 variables:
##  $ year : int  1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 ...
##  $ boys : int  5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 ...
##  $ girls: int  4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 ...

We can see that there are 82 observations and 3 variables in this dataset. The variable names are year, boys, and girls. 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 means supplying a function with some number of arguments. The str command, for example, took a single argument, the name of a data frame.

Some Exploration

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

arbuthnot$boys
##  [1] 5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 5366 5518 5470 5460
## [15] 4793 4107 4047 3768 3796 3363 3079 2890 3231 3220 3196 3441 3655 3668
## [29] 3396 3157 3209 3724 4748 5216 5411 6041 5114 4678 5616 6073 6506 6278
## [43] 6449 6443 6073 6113 6058 6552 6423 6568 6247 6548 6822 6909 7577 7575
## [57] 7484 7575 7737 7487 7604 7909 7662 7602 7676 6985 7263 7632 8062 8426
## [71] 7911 7578 8102 8031 7765 6113 8366 7952 8379 8239 7840 7640

This command will only show the number of boys baptized each year. The dollar sign basically says “go to the data frame that comes before me, and find the variable that comes after me”.

EXERCISE 1. What command would you use to extract just the counts of girls baptized? Try it!

SOLUTION: Enter your R code in the chunk below, then run the code.

arbuthnot$girls
##  [1] 4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 4784 5332 5200 4910
## [15] 4617 3997 3919 3395 3536 3181 2746 2722 2840 2908 2959 3179 3349 3382
## [29] 3289 3013 2781 3247 4107 4803 4881 5681 4858 4319 5322 5560 5829 5719
## [43] 6061 6120 5822 5738 5717 5847 6203 6033 6041 6299 6533 6744 7158 7127
## [57] 7246 7119 7214 7101 7167 7302 7392 7316 7483 6647 6713 7229 7767 7626
## [71] 7452 7061 7514 7656 7683 5738 7779 7417 7687 7623 7380 7288

Notice that the way R has printed these data is different. When we looked at the complete data frame, we saw 82 rows, one on each line of the display. These data are no longer structured in a table with other variables, so 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, 5218 follows [1], indicating that 5218 is the first entry in the vector. And if [43] starts a line, then that would mean the first number on that line would represent the 43rd entry in the vector.

Data visualization

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

ggplot() +
  geom_line(mapping = aes(x = year, y = girls), data = arbuthnot)

The plot should appear under the Plots tab of the lower right panel of RStudio. We’re going to really dive into understanding how to make plots next week. For now, notice four things about the command to make the plot:

  1. We use ggplot() + to start a new plot. The + at the end of the line signals that we’re going to add a layer to the plot.

  2. We use the geom_line() function to plot the data using a line

  3. The first argument to geom_line() tells R to map the year variable to the horizontal (x) axis and the girls variable to the vertical (y) axis.

  4. The second argument to geom_line() tells R that it can find those variables in the arbuthnot data frame.

EXERCISE 2. Is there an apparent trend in the number of girls baptized over the years? How would you describe it?

SOLUTION: Overall, there was an increasing trend in the number of girls baptized, with a noticeable dip for about 15 years between roughly 1645 and 1660.

R as a big calculator

Now, suppose we want to plot the total number of baptisms. To compute this, we could use the fact that R is really just a big calculator. We can type in mathematical expressions like

5218 + 4683
## [1] 9901

to see the total number of baptisms in 1629. We could repeat this once for each year, but there is a faster way. If we add the vector for baptisms for boys to that of girls, R will compute all sums simultaneously.

arbuthnot$boys + arbuthnot$girls
##  [1]  9901  9315  8524  9584  9997  9855 10034  9522  9160 10311 10150
## [12] 10850 10670 10370  9410  8104  7966  7163  7332  6544  5825  5612
## [23]  6071  6128  6155  6620  7004  7050  6685  6170  5990  6971  8855
## [34] 10019 10292 11722  9972  8997 10938 11633 12335 11997 12510 12563
## [45] 11895 11851 11775 12399 12626 12601 12288 12847 13355 13653 14735
## [56] 14702 14730 14694 14951 14588 14771 15211 15054 14918 15159 13632
## [67] 13976 14861 15829 16052 15363 14639 15616 15687 15448 11851 16145
## [78] 15369 16066 15862 15220 14928

What you will see are 82 numbers (in that packed display, because we aren’t looking at a data frame here), each one representing the sum we’re after. Take a look at a few of them and verify that they are right.

Adding a new variable to the data frame

We’ll be using this new vector to generate some plots, so we’ll want to save it as a permanent column in our data frame.

arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)

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).

A note on piping: Note that we can read these three lines of code as the following:

“Take the arbuthnot dataset and pipe it into the mutate function. Mutate the arbuthnot data set by creating a new variable called total that is the sum of the variables called boys and girls. Then assign the resulting dataset to the object called arbuthnot, i.e. overwrite the old arbuthnot dataset with the new one containing the new variable.”

This is equivalent to going through each row and adding up the boys and girls counts for that year and recording that value in a new column called total.

Where is the new variable? When you make changes to variables in your dataset, click on the name of the dataset again to update it in the data viewer.

You’ll see that there is now a new column called total that has 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 arbuthnot, 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

ggplot() +
  geom_line(mapping = aes(x = year, y = total), data = arbuthnot)

Similarly to how we computed the total number of births, we can compute the ratio of the number of boys to the number of girls baptized in 1629 with

5218 / 4683
## [1] 1.114243

or we can act on the complete columns with the expression

arbuthnot <- arbuthnot %>%
  mutate(boy_to_girl_ratio = boys / girls)

We can also compute the proportion of newborns that are boys in 1629

5218 / (5218 + 4683)
## [1] 0.5270175

or this may also be computed for all years simultaneously and append it to the dataset:

arbuthnot <- arbuthnot %>%
  mutate(boy_ratio = boys / total)

Note that we are using the new total variable we created earlier in our calculations.

EXERCISE 3. Now, generate a plot of the proportion of boys born over time. What do you see?

SOLUTION: Enter your R code in the chunk below, then run the code, and describe in words the trend you see.

ggplot() +
  geom_line(mapping = aes(x = year, y = boy_ratio), data = arbuthnot)

The trend in the proportion of babies who are baptized who are boys is basically flat or perhaps decreasing very slightly. In most years, between about 51% and 53% of baptisms were boys.

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 boys outnumber girls in each year with the expression

arbuthnot <- arbuthnot %>%
  mutate(more_boys = boys > girls)

This command add a new variable to the arbuthnot dataframe containing the values of either TRUE if that year had more boys than girls, or FALSE if that year did not (the answer may surprise you). This variable contains a different kind of data than we have encountered so far. All other columns in the arbuthnot data frame have values that are numerical (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.


More Practice

In the previous few pages, you recreated some of the displays and preliminary analysis of Arbuthnot’s baptism data. Your assignment involves repeating these steps, but for present day birth records in the United States. Load the present day data with the following command.

data(present)

The data are stored in a data frame called present.

EXERCISE 4. What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?

SOLUTION: This data set covers the years from 1940 - 2002 (I checked this by typing View(present) into the console). The data frame has 63 rows and 3 columns (from View(present) or dim(present)). The variable names are “year”, “boys”, and “girls” (from View(present) or names(present))

EXERCISE 5. How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?

SOLUTION: The counts for the present data frame are much larger than the counts for the arbuthnot data frame. For the arbuthnot data, the counts are generally less than about 10,000 for each of boys and girls. For the present data, the counts are closer to 1 or 2 million.

EXERCISE 6. In what year did we see the most total number of births in the U.S.?

Hint: First calculate the totals and save it as a new variable. You can do this by copying the code we used to calculate the totals for Arbuthnot’s data above, and modifying the data frame that is used in the calculation. Then, sort your dataset in descending order based on the total column. You can do this interactively in the data viewer by clicking on the arrows next to the variable names. 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). Sample code for this second step is provided below.

present %>%
  arrange(desc(total))

SOLUTION:

present <- present %>%
  mutate(total = boys + girls)
present <- present %>%
  arrange(desc(total))
head(present)
##   year    boys   girls   total
## 1 1961 2186274 2082052 4268326
## 2 1960 2179708 2078142 4257850
## 3 1957 2179960 2074824 4254784
## 4 1959 2173638 2071158 4244796
## 5 1958 2152546 2051266 4203812
## 6 1962 2132466 2034896 4167362

The highest total number of births in the US was in 1961.

EXERCISE 7. Make a plot that displays the proportion of boys born over time. What do you see? Does Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S.? Include the plot in your response.

Hint: You can base your code on the code above that calculated the boy_ratio variable. Copy the code above that we used to perform these calculations for the arbuthnot data set. Then, replace the dataframe name. Similarly, you should be able to modify the code you used for Exercise 3 to make the plot.

SOLUTION:

present <- present %>%
  mutate(boy_ratio = boys / total)
ggplot() +
  geom_line(mapping = aes(x = year, y = boy_ratio), data = present)

In this plot, we see a decreasing trend in the proportion of births who are boys. This proportion is generally just over 51%. This is consistent with the ratio we saw in Arbuthnot’s data.

Resources for learning R and working in RStudio

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”.

These cheatsheets may come in handy throughout the semester:

Note that some of the code on these cheatsheets may be too advanced for this course, however the majority of it will become useful throughout the semester.

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. It was then further modified by Evan Ray.