install.packages("tidyverse", dependencies = TRUE)
library(tidyverse)
install.packages("gapminder")
library(gapminder)
install.packages("gganimate", dependencies = TRUE)
install.packages("hexbin")
install.packages("Rcpp", dependencies = TRUE)
install.packages("devtools", dependencies = TRUE)
2 R for Data Analysis: the Basics
First, you’ll need to install a few more packages in your console in RStudio. Run this chunk of code:
If prompted, type “yes” in your console and then hit enter when you’re asked if you want to install from sources. Then you can run the following:
library(devtools)
install_github("cobriant/qelp")
This installs qelp (quick help): an alternative set of beginner friendly help docs I created (with contributions from previous students) for commonly used functions in R and the tidyverse. Once you have the package installed, you can access the help docs from inside RStudio. To test that everything worked, now run:
::install.packages ?qelp
If everything went right, the help docs I wrote on the function install.packages should pop up in the lower right hand pane. Whenever you want to read the qelp docs on a function, you type ?
, qelp
, two colons ::
which means “I want the help docs on this function which is from the package qelp”, and then the name of the function you’re wondering about.
Now, download the koans for learning R. Go here: https://github.com/cobriant/tidyverse_koans, hit the green button that says “Code” and then hit “Download ZIP”.
Find the file (probably in your downloads folder) and open it to unzip it. Navigate to the new folder named “tidyverse_koans-master
” and double click on the R project “tidyversekoans.Rproj
”. RStudio should open. If it doesn’t, open RStudio and go to File > Open Project
and then find tidyversekoans.Rproj
.
In RStudio, go to the lower righthand panel and hit the folder “R”. This takes you to the list of 20 koans. We’ll do about the first half in this course: if you want to learn more, you’re welcome to go beyond that. Open the first koan: K01_vector.R
. Before you start, modify a keybinding:
Macs: Tools > Modify keyboard shortcuts > Run a test file > Cmd Shift T
Windows: Tools > Modify keyboard shortcuts > Run a test file > Ctrl Shift T
Now hit Cmd/Ctrl Shift T
. You’ve just tested the first koan. You should see:
[ FAIL 0 | WARN 0 | SKIP 10 | PASS 0 ]
What does this mean? If there are errors in your R script, the test will not complete. Since it completed, you know there are no errors. Since FAIL
is 0, you also haven’t failed any of the questions yet. But PASS
is also 0, so you haven’t passed the questions either. Since they’re blank right now, the test will skip them. That’s why SKIP
is 10.
Go ahead and start working on the koans and learning about the tidyverse! When you’re finished with a koan and the tests pass, it can be nice to be able to see your work in a compiled html document (File > Compile Report
).
One last thing: whenever you want to work on the koans, make sure you open RStudio by opening the “tidyverse_koans-master
” project, not just the individual koan file. If you open the koans in a session that’s not associated with the “tidyverse_koans-master
” project, the tests will fail to run. You can always see which project your current session is being associated with by looking at the upper right hand corner of RStudio: if you’re in the “tidyverse_koans-master
” project, you’ll see “tidyverse_koans-master
” up there. That’s good. If you’re in no project at all, you’ll see “Project: (None)
” up there. That’s not good, especially if you want the tests to run. If you see “Project: (None)
”, just click that text and you’ll be able to switch over to the “tidyverse_koans-master
” project.
Before class on Tuesday, please complete koans 1-3: vectors, tibbles, and pipes. This reading may help you understand the concepts even better:
2.1 Classwork 2
For today’s homework, you should have completed Koans 1-3 on vectors, tibbles, and pipes. If you haven’t done so yet, I recommend starting there. Even though Koans 1-3 won’t be collected or graded, they introduce some crucial fundamentals.
Today’s classwork is to complete Koans 4, 5, and 6, which focus on the tidyverse package called dplyr
. The name “dplyr” is meant to suggest “data pliers,” implying that you use dplyr functions to manipulate your data. However, I find this analogy can be misleading, as it might cause confusion among students, leading them to think that their dataset is permanently altered when they apply dplyr functions (in truth, a variable only changes when you explicitly reassign it using <-
). Instead, it’s more helpful to view dplyr as a tool for writing queries on your data, just like how SQL works. Just remember, whenever you have a question about your data, dplyr is the tool you want to use.
Koans 4-6 are due as Classwork 2 on Friday at 5pm. To submit them, you should compile each one to html and then upload all three to blackboard as a zip file (one submission per group).