Photo by Jovana Askrabic on Unsplash
The goal of this assignment is to introduce you to R, Posit Cloud, and Quarto, which you’ll be using throughout the course to learn data science concepts, analyze real data, and communicate your results.
This assignment assumes that you have reviewed the lecture titled “Meet the toolkit: Programming”. If you haven’t yet done so, please pause and complete it before continuing.
We’ve already thrown around a few new terms, so let’s define them before we proceed.
R: The programming language we will be using throughout the course.
Posit Cloud: An online environment for working with R. It gives you a convenient interface for writing and running code, viewing output, and working with your course files.
Quarto: A document format that lets you combine written text, R code, and the output from your code in a single document.
Render/Knit: The process of running the code in your Quarto document and creating the finished output document.
As the course progresses, you are encouraged to explore beyond what the assignments dictate; a willingness to experiment will make you a much better programmer! Before we get to that stage, however, you need to build some basic fluency in R and Posit Cloud.
Before you get started, make sure that you are a member of the course Posit Cloud workspace.
For assignments in this course, you will work in a project in our Posit Cloud workspace. The basic workflow will be:
Open assignment → Work in Posit Cloud → Run code → Render → Check → Submit
As you work, run your code frequently so that you can see what it does and catch errors early. Render your Quarto document periodically to make sure that your code, output, figures, and written responses appear correctly in the finished document.
Go to posit.cloud and navigate to the course workspace via the left sidebar. It’s important that you work in the course workspace so that you have access to the course projects and the R packages that have been set up for you.
Open the project for this assignment.
Posit is comprised of four panes.
2 + 2 here and hit enter, what do you get?x <- 2 in the Console and hit enter. What do you see in the Environment pane?Before we introduce the data, let’s warm up with some simple exercises.
The top portion of your Quarto file (between the three dashed lines) is called YAML. It stands for “YAML Ain’t Markup Language”. It is a human friendly data serialization standard for all programming languages. All you need to know is that this area is called the YAML (we will refer to it as such) and that it contains meta information about your document.
Open the Quarto (Rmd or qmd) file in your project, change the author name to your name, and knit the document.
After you knit the document, open the rendered output and check that your name appears correctly and that the document renders without errors.
Throughout the assignment, use this same habit: make a change, run your code, render the document, and check the result.
R is an open-source language, and developers contribute functionality to R via packages. In this assignment we will use the following packages:
We use the library() function to load packages.
In your Quarto document you should see an R chunk labelled load-packages which has the necessary code for loading both packages.
You should also load these packages in your Console, which you can do by sending the code to your Console by clicking on the Run Current Chunk icon (green arrow pointing right icon).
Note that these packages are also get loaded in your Quarto environment when you Render your Quarto document.
The city of Seattle, WA has an open data portal that includes pets registered in the city.
For each registered pet, we have information on the pet’s name and species.
The data used in this exercise can be found in the openintro package, and it’s called seattlepets.
Since the dataset is distributed with the package, we don’t need to load it separately; it becomes available to us when we load the package.
You can view the dataset as a spreadsheet using the View() function.
Note that you should not put this function in your Quarto document, but instead type it directly in the Console, as it pops open a new window (and the concept of popping open a window in a static document doesn’t really make sense…).
When you run this in the console, you’ll see the following data viewer window pop up.
You can find out more about the dataset by inspecting its documentation (which contains a data dictionary, name of each variable and its description), which you can access by running ?seattlepets in the Console or using the Help menu in Posit to search for seattlepets.
The ✏️ symbol is a reminder to write a written response discussing the questions in the exercises.
🧶 ✅ Write your answer under Exercise 1 and render the document.
🧶 ✅ Write your answer under Exercise 2 and render the document.
The two lines of code can be read as “Start with the seattlepets data frame, and then count the animal_names, and display the results sorted in descending order. The”and then” in the previous sentence maps to |>, the pipe operator, which takes what comes before it and plugs it in as the first argument of the function that comes after it.
🧶 ✅ Complete the code in the code chunk provided, run the code to examine the output, and write your answer under Exercise 3. Then render the document and check that your code, output, and written response appear correctly.
Let’s also look to see what the most common pet names are for various species.
For this we need to first group_by() the species, and then do the same counting we did before.
Looks like many of those NAs were cats. Poor unnamed kitties…
## # A tibble: 16,823 × 3
## # Groups: species [4]
## species animal_name n
## <chr> <chr> <int>
## 1 Cat <NA> 406
## 2 Dog Lucy 337
## 3 Dog Charlie 306
## 4 Dog Bella 249
## 5 Dog Luna 244
## 6 Dog Daisy 221
## # ℹ 16,817 more rows
But this output isn’t exactly what we wanted. We wanted to know the most common cat and dog names, but there are barely any cats present in this output! This is because there are more dogs than cats in the dataset overall. We can confirm this by counting the various species in the data.
6 pigs in the city? Ok… But we’ll continue with cats and dogs.
## # A tibble: 4 × 2
## species n
## <chr> <int>
## 1 Dog 35181
## 2 Cat 17294
## 3 Goat 38
## 4 Pig 6
Let’s search for the top 5 cat and dog names.
To do this, we can use the slice_max() function.
The first argument in the function is the variable we want to select the highest values of, which is n.
The second argument is the number of rows to select, which is n = 5 for the top 5.
It may be a bit confusing that both of these are n, but this is because we already have a variable called n in the data frame.
## # A tibble: 53 × 3
## # Groups: species [4]
## species animal_name n
## <chr> <chr> <int>
## 1 Cat <NA> 406
## 2 Cat Luna 111
## 3 Cat Lucy 102
## 4 Cat Lily 86
## 5 Cat Max 83
## 6 Dog Lucy 337
## # ℹ 47 more rows
n (the frequencies) as opposed to being organized by the species. Build on the pipeline to arrange the results so that they’re arranged by species first, and then n. This means you will need to add one more step to the pipeline, and you have two options: arrange(species, n) or arrange(n, species). You should try both and discuss which one organizes the output by species and then ranks the names in order of frequency for each species.🧶 ✅ Complete the code in the code chunk provided, run the code to examine the output, and write your answer under Exercise 4. Then render the document and check that your code, output, and written response appear correctly.
The following visualization plots the proportion of dogs with a given name versus the proportion of cats with the same name. The 20 most common cat and dog names are displayed. The diagonal line on the plot is the \(x = y\) line; if a name appeared on this line, the name’s popularity would be exactly the same for dogs and cats.
🧶 ✅ Render your document one final time. Review the rendered document to make sure all of your answers, code, output, and figures appear correctly before submitting your work.