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).
Today you will be working on R practice problems. Answers are posted on that page.
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.
- 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
- Install R Studio (Desktop Version), choose the “Free” option
(This will also be posted on the Reference page.)
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
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
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:
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.
We know vectors can be created with the
c() command (and stored with
<-), but there are other shortcuts to combine objects into a vector, particularly numeric data:
:creates a series of integers
##  1 2 3 4 5
##  12 13 14 15 16 17
seq(from = , to = , by = )creates a numeric sequence, and is not restricted to integers
##  1 3 5 7 9
##  32.5 34.0 35.5 37.0 38.5 40.0
rep(., times =)The
.is a placeholder here.
repeats an element a specified number of times
##  2 2 2 2
##  2 2 2 2
##  1 4 7 1 4 7 1 4 7
- We can combine these:
##  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.
- comment above for overall idea
- comment on side for individual elements of long commands
- name with
- spaces betweeen all operators:
- line breaks between multiple arguments to a function