1.2 — Meet R — Class Notes


Thursday, August 20, 2020


Today we begin the long slog to your mastery of R. We begin with the basics - how R works, how to use it, the different data types, and how to create and manipulate objects.


Please see today’s readings for more information.


Live Class Session on Zoom

The live class Zoom meeting link can be found on Blackboard (see LIVE ZOOM MEETINGS on the left navigation menu), starting at 11:30 AM.

If you are unable to join today’s live session, or if you want to review, you can find the recording stored on Blackboard via Panopto (see Class Recordings on the left navigation menu).

Practice Problems

Today you will be working on R practice problems. Answers are posted on that page.


Preliminary Survey

Please take the preliminary survey on your statistics and software background by 11:59 PM Sunday. This will help us all have a productive course together.

Additional Useful Information

Installing R and R Studio

We will be using Rstudio.cloud, for which you have made a free account. However, since R is free, I strongly recommend you download and install it on your own computers. You may decide also you prefer to use your own computers in class when we work with R.

  1. Install R from CRANThe Comprehensive R Archive Network

    by clicking “Download R” (or the CRAN link under Downloads on the left menu). This will take you to a mirrors page, where you can select a location in the U.S. and download a copy of R
  2. Install R Studio (Desktop Version), choose the “Free” option

(This will also be posted on the Reference page.)

R Packages

Packages come from multiple sources.

The polished, publicly released versions are found on CRAN. When installing a package available on CRAN, it is sufficient simply to tell R the following:Note the plural s on packages, and the quotes around the “package name”


Other packages, which may be in various developmental states (including perfectly functional!) are often hosted on GitHub before they make their way onto CRAN. Simply telling R install.packages("packagename") will fail to find it (as R only looks in CRAN for packages), so you must use another package called devtoolsWhich you will need to install first if you (probably) don’t already have it!

to install packages directly from Github:Note the :: allows you to use the function install_github() from the devtools package without having to first load the devtools package with library(devtools).


For example, to install Hadley Wickham’s package r4ds from its Github page https://github.com/hadley/r4ds, we would type:


To use a package, you need to ensure it is loaded to your workspace (you only need to do this once)^[When we learn how to write R Markdown documents, . with library("package_name").Quotes are not necessary this time.

Getting Help for R

For specific functions or commands, you can simply type:


# example

This will display a help page specific to that function in the Viewer pane. R functions and packages are extremely well-documented; help pages normallyThis useful guide comes from Kieran Healy’s excellent (free online!) book on Data Visualization.

a short description of the function, arguments and options (as well as their default values), and several examples or vignettes to demonstrate usage of the function.

Additionally, you can turn to the community by searching on Google or better yet, StackExchange.

Other Useful Commands to Know

One of the best/worst things about R is that it is a language, meaning there are multiple ways that you can accomplish the same task. Here are a few alternative methods relevant to what we have learned so far that might prove useful.

Creating Vectors

We know vectors can be created with the c() command (and stored with = or <-), but there are other shortcuts to combine objects into a vector, particularly numeric data:

  1. : creates a series of integers
1:5 # create a vector of 1 through 5
## [1] 1 2 3 4 5
12:17 # create a vector of 12 through 17
## [1] 12 13 14 15 16 17
seq(from = 1, to = 10, by = 2) # sequence from 1 to 10, by 2s
## [1] 1 3 5 7 9
# note you do not need to fully write out the name of each argument, just the input!

seq(32.5,40,1.5) # sequence from 32.5 to 40, by 1.5
## [1] 32.5 34.0 35.5 37.0 38.5 40.0
  1. rep(., times =)The . is a placeholder here.

    repeats an element a specified number of times
rep(2, times = 4) # repeat "2" four times
## [1] 2 2 2 2
rep(2, 4) # does the same thing
## [1] 2 2 2 2
# the thing repeated could itself be a vector
rep(c(1,4,7), 3) # repeat the vector "1, 4, 7" three times
## [1] 1 4 7 1 4 7 1 4 7
  1. We can combine these:
# combine (the sequence of 4 to 8 by 2's repeated three times) and 1 and 5

##  [1] 4 6 8 4 6 8 4 6 8 1 5

Suggested Style Guide for Coding

We want to maximize human-readability of code, not just machine-readability. I try to follow Hadley Wickham’s style guide for all of my code, including that in this class.

You will not be graded on the style of your code. But now’s the best time to learn best practices (while you don’t know any alternatives!) to save yourself and your potential colleagues (including your future self) from unnecessary frustration.

p<-ggplot(data=data, aes(x=x,y=y,fill=fill))+geom_point()


p <- ggplot(data = data,
       aes(x = x,
           y = y,
           fill = fill))+