
Introduction to R and the tidyverse
Part 1: The basics of R and {dplyr}
For the first part of this week’s lesson, work through the introductory materials below. You’ll do these in your browser, where you can type R code and see the results.
These materials introduce some of the basics of R, along with useful tools for working with data using the {dplyr} package.
There are several sections, but they’re short and go fairly quickly. I have no way of seeing what you do or what you get wrong or right, and that’s totally fine! The goal is to practice and become familiar with R. If you get stuck on something, you can move on and come back to it later.
- The Basics
-
Note: Some videos embedded in the Programming Basics page are no longer available from the original source. The rest of the page is still useful, so continue using it. You can use the following videos as additional references for these topics:
- Work with Data
The content from these primers comes from the (free and online!) book R for Data Science by Garrett Grolemund and Hadley Wickham. I highly recommend the book as a reference and for continuing to learn and use R in the future (like running regression models and other types of statistical analysis)
Part 2: Getting familiar with RStudio
The RStudio primers you just worked through are a great introduction to writing and running R code, but you typically won’t type code in a browser when you work with R. Instead, you’ll use a nicer programming environment like RStudio, which lets you type and save code in scripts, run code from those scripts, and see the output of that code, all in the same program.
To get familiar with RStudio, watch this video:
Part 3: RStudio Projects
One of the most powerful and useful aspects of RStudio is its ability to manage projects.
When you first open R, it is “pointed” at some folder on your computer, and anything you do will be relative to that folder. The technical term for this is a “working directory.”
When you first open RStudio, look in the area right at the top of the Console pane to see your current working directory. Most likely you’ll see something cryptic: ~/
That tilde sign (~) is a shortcut that stands for your user directory. On Windows this is C:\Users\your_user_name\; on macOS this is /Users/your_user_name/. With the working directory set to ~/, R is “pointed” at that folder, and anything you save will end up in that folder, and R will expect any data that you load to be there too.
It’s always best to point R at some other directory. If you don’t use RStudio, you need to manually set the working directory to where you want it with setwd(), and many R scripts in the wild include something like setwd("C:\\Users\\bill\\Desktop\\Important research project") at the beginning to change the directory. THIS IS BAD THOUGH (see here for an explanation). If you ever move that directory somewhere else, or run the script on a different computer, or share the project with someone, the path will be wrong and nothing will run and you will be sad.
The best way to deal with working directories with RStudio is to use RStudio Projects. These are special files that RStudio creates for you that end in a .Rproj extension. When you open one of these special files, a new RStudio instance will open up and be pointed at the correct directory automatically. If you move the folder later or open it on a different computer, it will work just fine and you will not be sad.
Read this super short chapter on RStudio projects.
In general, you can create a new project by going to File > New Project > New Directory > Empty Project, which will create a new folder on your computer that is empty except for a single .Rproj file. Double click on that file to open an RStudio instance that is pointed at the correct folder.
Part 4: Getting familiar with Quarto
To ensure that the analysis and graphics you make are reproducible, you’ll do the majority of your work in this class using Quarto files.
Do the following things:
Skim through the content at these pages. Again, lots of these resources mention R Markdown, which is Quarto’s predecessor, but the same principles apply to Quarto
Watch this video: