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. This is going to feel very unfamiliar, so feel free to ask a lot of questions. These labs will build on each other. You will want to refer back to previous labs often to remind yourself how to perform certain tasks.

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.

The panel on the lower left is where the action happens. It’s called the console. Every time you access 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, indicated by the > symbol. 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 environment as well as a history of the commands that you’ve previously entered.

The panel in the lower right contains tabs for browse the files in your project folder, access help files for R functions, install and manage R packages, and inspecting visualizations. By default, all data visualizations you make will appear directly below the code you used to create them. If you would rather your plots appear in the plots tab, you will need to change your global options.

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

To begin, let’s install these two packages by copying and pasting or typing the following two lines of code into the console 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, communicating the process R is going through to install the package from the location you selected when you installed R. If you were not prompted to select a server for downloading packages when you installed R, RStudio may prompt you to select a server from which to download; any of them will work.

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. Copy and paste or type the the following two lines in your console 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.

Creating a reproducible lab report

We will be using R Markdown to create reproducible lab reports. See the following short video describing why and how:

Why use R Markdown for Lab Reports?

To help get you started, a template has been provided for you. In RStudio, go to New File -> R Markdown… Then, choose “From Template” and then choose Lab Report for OpenIntro Statistics Lab 1 from the list of templates. Give this file a name with your name and lab number, something like Lab1_Your_Name.Rmd.

I will refer to this as your “Markdown file” or “your report”. This is a template where you can see where to type code and where to type text. You’ll need to figure out whether code is needed to answer a particular question, and if so a new chunk of code can be inserted by clicking on the Insert R button (dropdown menu under Insert on the upper right corner of your markdown document).

Before you keep going type your full name and lab # in the appropriate spot at the top of the lab report (called the header). Then click on Knit HTML (see a dropdown box above) and you’ll see your document in a new pop-up window. If you have pop-ups blocked on your laptop, you may see a box come up warning you. Just click Try Again and you should see the results.

Going forward we will type most of our code in the lab report and not in the console, as this makes it easier to remember and reproduce the output you want to reference. Potentially the most important feature of R Markdown files is that they allow for us to nest our R code within a written report. In an R Markdown file, R code appears in a gray box, which we call “code chunks.” The R Markdown file knows that the gray box contains R code because it begins with three tick marks (```), followed by two curly braces that contain a lowercase letter r ({r}). You’ve already seen this above!

Instead of typing our R code into the console, we encourage you to type any code you produce (final correct answer, or anything you’re just trying out) in the R code chunk associated with each problem. You can execute the R code you type in these code chunks similar to how you typed code into the console and pressed enter/return. 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, if you wanted to run all of the R code in a given code chunk, you can click on the “Play” button in the upper right hand corner of the code chunk (green sideways triangle).

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.

Dr. Arbuthnot’s Baptism Records

To get started, let’s take a peek at the data.

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: 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 a data set called arbuthnot that 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 and suspected that the ratio was not 50/50, 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 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

This command does display the data for us, 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. The Environment tab (in the upper right pane) 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 in the upper left hand corner.

When inspecting the data, you should see four columns of numbers and 82 rows. Each row represents a different year that Arbuthnot collected data. The first entry in each row is 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 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.

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)

Although we previously said that it is best practice to type all of your R code into the code chunk, it is better practice to type this command into your console. Generally, you should type all of the code that is necessary for your solution into the code chunk. Because this command is used to explore the data, it is not necessary for your solution code and should not be included in your solution file.

This command should output the following:

## 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 separately using a command like

arbuthnot$boys

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

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

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(data = arbuthnot, aes(x = year, y = girls)) + 
  geom_point()

We use the ggplot() function to build plots. If you run the plotting code in your console, you should see the plot appear under the Plots tab of the lower right panel of RStudio. Notice that the command above again looks like a function, this time with arguments separated by commas.

With ggplot():

  • The first argument is always the dataset.
  • Next, you provide the variables from the dataset to be assigned to aesthetic elements of the plot, e.g. the x and the y axes.
  • Finally, you use another layer, separated by a + to specify the geometric object for the plot. Since we want a scatterplot of points, we use geom_point().

For instance, if you wanted to visualize the above plot using a line graph, you would replace geom_point() with geom_line().

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

Use the plot to answer the following question:

  1. Is there an apparent trend in the number of girls baptized over the years? How would you describe it? (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.) You might wonder how you are supposed to know the syntax for the ggplot function. Thankfully, R documents all of its functions extensively. To learn what a function does and its 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 in your console (not your report):
?ggplot

Notice that the help file replaces the plot in the lower right panel. You can toggle between plots and help files using the tabs at the top of that panel.

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

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

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 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 again to update it in the data viewer.

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 one line of code and saving it into an object in your environment. In this case, you already have an object called arbuthnot, so this command updatesthat data set with the new mutated column.

You can make a line plot of the total number of baptisms per year with the command

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

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

5218 / 4683

or you can act on the complete columns with the expression

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

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

5218 / (5218 + 4683)

or you can compute this 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.

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

Tip: If you use the up and down arrow keys in the console, 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 by allowing you to copy and paste previously used code.

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

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

This command adds 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 with R

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. The data are stored in a data frame called present.

To find the minimum and maximum values of columns, you can use the functions min and max within a summarize() call, which you will learn more about in the following lab. Here’s an example of how to find the minimum and maximum amount of boy births in a year:

arbuthnot %>%
  summarize(min = min(boys), max = max(boys))
  1. What years are included in the present data set? What are the dimensions of the data frame? What are the variable (column) names?

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

  3. 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 should be able to reuse your code from Exercise 3 above, just replace the dataframe name.

  4. 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. 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). The sample code is provided below.

present %>%
  arrange(desc(total))

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.

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

These cheat sheets may come in handy throughout the semester:

Note that some of the code on these cheat sheets may be too advanced for this course. However the majority of it will become useful throughout the semester.

Turning in your lab report

After you finish answering all the questions, you should download the .html file of the knitted document. You can submit this file to Canvas under the assignment for Lab 1.

Make sure to look at your final report and ensure it is readable! In your final report, you should only include code necessary to answer the questions.


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.