The RStudio Interface

The goal of this lab is to continue introducing 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 for working with R .

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. First, we will explore the fundamental building blocks of R and RStudio: the RStudio interface, reading in data, and basic commands for working with data in R.

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:

  • The tidyverse “umbrella” package which houses a suite of many different R packages: for data wrangling and data visualization
  • The openintro R package: for data and custom functions with the OpenIntro resources that our textbook uses

In the lower right hand quadrant of RStudio click on the Packages tab. Type the name of each of these packages (tidyverse, openintro) into the search box to see if they have been installed. If these packages do not appear when you type in their name, install them by copying and pasting or typing the following two lines of code into the console (bottom left quadrant) of your RStudio session. Be sure to press enter/return after each line of code.

install.packages("tidyverse")
install.packages("openintro")

After pressing enter/return, a stream of text will begin printing out in the console, communicating the process R is going through to install the package. You only need to install packages once, but you need to load them each time you relaunch RStudio. We load packages with the library function. Run the following two lines of code by clicking on the “Play” button in the upper right hand corner of the code chunk (green sideways triangle) to load the tidyverse and openintro packages into your working environment.

library(tidyverse)
library(openintro)

We are choosing to use the tidyverse package because it consists of a set of packages necessary for different aspects of working with data, anything from loading data to wrangling data to visualizing data to analyzing data. Additionally, these packages share common philosophies and are designed to work together. You can find more about the packages in the tidyverse at tidyverse.org.

A note on “running code”

Within the code chunk there are two ways to execute a line of R code: (1) place your cursor on the line on code and press Ctrl-Enter or Cmd-Enter at the same time, or (2) place your cursor on the line and press the “Run” button in the upper right hand corner of the R Markdown file. Alternatively, clicking the “Play” button as you did above will run all of the R code in a given code chunk.

If at any point you need to start over and run all of the code chunks before a specific code chunk, you click on the “Fastforward” button in the upper right hand corner of that code chunk (gray upside down triangle with a bar below). This will run every code chunk that occurred before that code chunk, but will not execute the R code included in that code chunk. If you ever get an error that says “object not found,” sometimes that is an indication that you haven’t run the code up above that is needed to run the current code, so pressing the “Fastforward” button is a good first trouble-shooting step.

Some quick Settings changes

Click on the gear/sprocket icon next to the Knit button. Select “Chunk Output in Console.” If a warning pops up, you can click “Remove output.” Again under the gear/sprocket menu, make sure that “Preview in Viewer Pane” is selected.

Getting Started: Dr. Arbuthnot’s Baptism Records

To get started, let’s take a peek at the data by running the following code

data(arbuthnot)

Again, you can run the code above by:

  • placing your cursor on the line and pressing Ctrl-Enter or Cmd-Enter
  • placing your cursor on the line and pressing the “Run” button in the upper right hand corner of the R Markdown file, or
  • by clicking on the green arrow at the top right hand corner of the code chunk

The single line of code included in this code chunk instructs R to load some data from the openintro package: the Arbuthnot baptism counts for boys and girls. You should see that the Environment tab in the upper right hand corner of the RStudio window now lists arbuthnot. If you click on it, you will see that arbuthnot has 82 observations on 3 variables. As you interact with R, you will create objects for a variety of purposes. Sometimes you load the objects into your workspace by loading a package, as we have done here, but sometimes you create objects yourself as a byproduct of a computation process, for an analysis you have performed, or for a visualization you have created.

The Arbuthnot data set refers to the work of 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. Once again, we can view the data by running the code below or by typing the name of the dataset into the console. Be careful with the spelling and capitalization you use! R is case sensitive, so if you accidentally type Arbuthnot R will tell you that object cannot be found.

arbuthnot
## # A tibble: 82 × 3
##     year  boys girls
##    <int> <int> <int>
##  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
## # … with 72 more rows

When inspecting the data that prints out in the console, you should see three columns of numbers and an indication that there are 82 rows. Each row represents a different year that Arbuthnot collected data. The first entry in each row is the year, and the second and third are the numbers of boys and girls baptized that year, respectively. Note that the row numbers to the left of the first column are not part of Arbuthnot’s data. R adds these row numbers 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 of the data to a spreadsheet will generally be helpful. R has stored Arbuthnot’s data in an object similar to a spreadsheet or a table, which R calls a data frame.

Often, printing the dataset in the console is not that useful. One advantage of RStudio is that it comes with a built-in data viewer. The Environment tab (in the upper right quadrant of RStudio) lists the objects in your environment. Clicking on the name arbuthnot will open up a Data Viewer tab next to your R Markdown file, which provides an alternative display of the data set. This display should feel similar to viewing data in Excel, where you are able to scroll through the dataset to inspect it. However, unlike Excel, you will not be able to edit the data in this tab. Once you are done viewing the data, you can close this tab by clicking on the x.

You can see the dimensions of this data frame as well as the names of the variables and the first few observations by inserting the name of the dataset into the glimpse() function, as seen below:

glimpse(arbuthnot)
## Rows: 82
## Columns: 3
## $ year  <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639…
## $ boys  <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5366…
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4784…

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 inputs (what are called arguments) which the function uses to produce an output. The glimpse() command, for example, took a single argument, the name of a data frame and produced a display of the dataset as an output.

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 by extracting the column with a $. For example, the code below extracts the boys column from the arbuthnot data frame.

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

This command will only show the number of boys baptized each year. R interprets the $ as saying “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. When we looked at the complete data frame, we saw 82 rows, one on each line of the display. These data have been extracted from the data frame, so they are no longer structured in a table with other variables. Instead, these data are displayed one right after another. Objects that print out in this way are called vectors; similar to the vectors you may have seen in mathematics courses, vectors represent a list of numbers. R has added numbers displayed in [brackets] along the left side of the printout to indicate each entry’s location within the vector. For example when you print out the boys column, 5218 follows [1], indicating that 5218 is the first entry in the vector. If [43] was displayed at the beginning of a line, that indicate that the first number displayed on that line would correspond to the 43rd entry in that vector.

Exercise 1 (1pt)

What command would you use to extract just the counts of girls baptized?

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

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 following code:

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

In this code, we use the ggplot() function to build a plot. The command above also looks like a mathematical function. This time, however, the function requires multiple inputs (arguments), which are separated by commas.

With ggplot():

  • The first argument is always the name of the dataset you wish to use for plotting.
  • Next, you provide the variables from the dataset to be assigned to different aesthetic elements of the plot, such as the x and the y axes.

These commands will build a blank plot, with the variables you assigned to the x and y axes. Next, you need to tell ggplot() what type of visualization you would like to add to the blank template. You add another layer to the ggplot() by:

  • adding a + at the end of the line, to indicate that you are adding a layer
  • then specify the geometric object to be used to create the plot.

Since we want to scatterplot, we use geom_point(). This tells ggplot() that each data point should be represented by one point on the plot. If you wanted to visualize the above plot using a line graph instead of a scatterplot, you would replace geom_point() with geom_line(). This tells ggplot() to draw a line from each observation with the next observation (sequentially).

Exercise 2 (2pts)

Create a line graph of the number of girls baptized per year. Remember: copy, paste, and tweak code from above.

ggplot(data = arbuthnot, aes(x = arbuthnot$year, y = arbuthnot$girls, type = "1")) + 
  geom_point()
## Warning: Use of `arbuthnot$year` is discouraged. Use `year` instead.
## Warning: Use of `arbuthnot$girls` is discouraged. Use `girls` instead.

Use the plot to address the following question:

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

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 we can use R as a big calculator. To do this, we can type in mathematical expressions such as the below calculation into the console.

5218 + 4683
## [1] 9901

This calculation provides us with the total number of baptisms in 1629 (9901 baptisms). We could then repeat this calculation once for each year. This would probably take us a while, but luckily there is a faster way! If we add the vector for baptisms for boys to that of girls, R can compute each of these sums simultaneously.

arbuthnot$boys + arbuthnot$girls

When you run the above code, you will see a list of 82 numbers. These numbers appear as a list, because we are working with vectors rather than a data frame. Each number represents the sum of how many boys and girls were baptized that year. You can take a look at the first few rows of the boys and girls columns to see if the calculation is right.

Adding a new variable to the data frame

We are interested in using this new vector of the total number of baptisms to generate some plots, so we’ll want to save it as a permanent column in our data frame. We can do this using the following code:

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

This code has a lot of new pieces to it, so let’s break it down. Here we are doing two things, (1) adding a new total column to this updated data frame, and (2) overwriting the existing arbutnot data frame with an updated data frame that includes the new total column. We are able to chain these two processes together using the piping (%>%) operator. The piping operator takes the output of the previous expression and “pipes it” into the first argument of the next expression.

To continue our analogy with mathematical functions, x %>% f(y) is equivalent to f(x, y). Connecting arbuthnot and mutate(total = boys + girls) with the pipe operator is the same as typing mutate(arbuthnot, total = boys + girls), where arbuthnot becomes the first argument included in the mutate() function.

A note on piping: Note that we can read these two 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 in your Environment again to view the updated dataset.

You’ll see that there is now a new column called total that has been tacked onto the data frame. The special symbol <- performs an assignment, taking the output of the piping operations and saving it into an object in your environment. In this case, you already have an object called arbuthnot in your environment, so this command updates that data set with the new mutated column.

Now that the arbuthnot dataset includes a total column, you can use that column to make a line plot of the total number of baptisms per year with the following code:

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

In an similar fashion, once you know the total number of baptisms for boys and girls in 1629, you could calculate the proportion of newborns that are boys in 1629 with the following code:

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

Or you can compute this for all years simultaneously and add it as a new variable named boy_ratio to the dataset:

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

Notice that rather than dividing by boys + girls we are using the total variable we created earlier in our calculations!

Exercise 3 (2pts)

Use the new boy_ratio variable to generate a line plot of the proportion of boys born over time.

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

Comment on what you see in the line plot.

From about 1660 it skews left.

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 create a new variable called more_boys that tells us whether the number of births of boys outnumbered that of girls in each year with the following code:

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

This command adds a new variable to the arbuthnot data frame 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

So far, you’ve recreated some of the displays and preliminary analysis of Arbuthnot’s baptism data. Now you will repeat many of these steps, but for present day birth records in the United States. The data are stored in a data frame called present. The following code loads the data.

data(present)

These data come from reports by the Centers for Disease Control. You can learn more about them by bringing up the help file using the command ?present.

Exercise 4 (4pts)

Inspect the new dataset in your Environment and then answer the following questions.

What years are included in this present data set?

The years 1940 - 2002

What are the dimensions of the data frame? That is, how many observations and variables does it have?

3

What are the variable (column) names?

Year, boys girls.

How do these counts in the boys and girls columns compare to the counts Arbuthnot’s data? Are they of a similar magnitude?

Counts are exceptionally larger.

Exercise 5 (2pts)

Create two new variables in the present dataset that compute the total births and the boy_ratio in each year. (Copy, paste, tweak the code used above that accomplished this for the arbuthnot dataset).

```{r} ggplot(data = present, aes(x = year, y = boys / total)) + mutate(total = boys + girls) %>%

Exercise 6 (2pts)

()



What do you see? Does Arbuthnot's observation about boys being born in greater proportion than girls hold up in the U.S.? 

> It seems that they were correct.

:::

::: {#boxedtext}
### Exercise 7 (1pt)

In what year did we see the most total number of births in the U.S.? *Hint:* 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 sort data using code, we use the function `arrange()` to sort the variable. Then we can arrange the data in a descending order with another function, `desc()`, for descending order. The sample code is provided below.


```r
present %>%
  arrange(desc(total))

1961

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 the suite of R packages from the tidyverse. The book R For Data Science by Grolemund and Wickham is a fantastic resource for data analysis in R with the tidyverse. If you are Goggling for R code, make sure to also include these package names in your search query. For example, instead of Goggling “scatterplot in R”, Goggle “scatterplot in R with the tidyverse”.

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