1.2 — Meet R — Class Content

Thursday, August 26, 2021

Please complete the preliminary survey by Sunday August 29.

Overview

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.

Readings

Now that we start working with R, you should consider this book to be your primary reference for R-related questions. We will broadly cover the first few chapters in order over the next 2-3 class periods.

Slides

Below, you can find the slides in two formats. Clicking the image will bring you to the html version of the slides in a new tab. Note while in going through the slides, you can type h to see a special list of viewing options, and type o for an outline view of all the slides.

The lower button will allow you to download a PDF version of the slides. I suggest printing the slides beforehand and using them to take additional notes in class (not everything is in the slides)!

1.2-slides

Download as PDF

R Practice

Today we will be working on practice problems. Answers will be posted later on that page.

Assignments

Preliminary Statistics Survey Due Sunday August 29

Please take the preliminary survey on your statistics and software background by 11:59 PM Sunday. This will help us all have a productive semester 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 CRAN1 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:2

install.packages("packagename") 

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 devtools3 to install packages directly from Github:4

devtools::install_github("username/packagename") 

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

devtools::install_github("hadley/r4ds")

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

Getting Help for R

For specific functions or commands, you can simply type:

?functionname()

# example
?mean()

This will display a help page specific to that function in the Viewer pane. R functions and packages are extremely well-documented; help pages normally6 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 = , to = , by = ) creates a numeric sequence, and is not restricted to integers
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 =)7 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

c(rep(seq(4,8,2),3),1,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.

  • comment above for overall idea
  • comment on side for individual elements of long commands
  • name with _
  • use %>% wherever possible
  • spaces betweeen all operators: <-, =, +, etc.
    • Exception: : and ::
  • line breaks between multiple arguments to a function
p<-ggplot(data=data, aes(x=x,y=y,fill=fill))+geom_point()

becomes

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

  1. The Comprehensive R Archive Network↩︎

  2. Note the plural s on packages, and the quotes around the “package name”↩︎

  3. Which you will need to install first if you (probably) don’t already have it!↩︎

  4. 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).↩︎

  5. Quotes are not necessary this time.↩︎

  6. This useful guide comes from Kieran Healy’s excellent (free online!) book on Data Visualization.↩︎

  7. The . is a placeholder here.↩︎

Previous
Next