Lab 1
Exploring NC Counties
Introduction
This lab will introduce you to the course computing workflow. One of the mail goals of this lab is to reinforce our demo of R and RStudio, which we will be using throughout the course both to learn the statistical concepts discussed in the course and to analyze real data and come to informed conclusions. The other main goal is meet other students in the class, get to know them, and work together with them.
R is the name of the programming language itself and RStudio is a convenient interface, commonly referred to as an integrated development environment or an IDE, for short.
An additional goal is to reinforce Git and GitHub, the version control, web hosting, and collaboration systems that we will be using throughout the course.
Git is a version control system (like “Track Changes” features from Microsoft Word but more powerful) and GitHub is the home for your Git-based projects on the internet (like DropBox but much better).
As the labs progress, you are encouraged to explore beyond what the labs 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. Today we begin with the fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.
This lab assumes that you have already completed Lab 0. If you have not, please
- go back and do that first before proceeding and
- let your TA know as they will need to set up a Lab 1 repository for you before you can complete this lab.
Learning objectives
- Gain practice with
- the data science workflow using R, RStudio, Git, and GitHub,
- writing a reproducible report using Quarto, and
- version control using Git and GitHub.
- Create data visualizations with the
tidyverse
, specifically usingggplot2
. - Write a simple data transformation pipeline with the
tidyverse
, specifically usingdplyr
.
Getting started
Step 1: Log in to RStudio
- Go to https://cmgr.oit.duke.edu/containers and login with your Duke NetID and Password.
- Click
STA199
under My reservations to log into your container. You should now see the RStudio environment.
Refresher: R and R Studio
Below are the components of the RStudio IDE.
Below are the components of a Quarto (.qmd) file.
Step 2: Clone the repo & start new RStudio project
Go to the course organization at github.com/sta199-f25 organization on GitHub. Click on the repo with the prefix lab-1. It contains the starter documents you need to complete the lab.
Click on the green CODE button, select Use SSH (this might already be selected by default, and if it is, you’ll see the text Clone with SSH). Click on the clipboard icon to copy the repo URL.
In RStudio, go to File ➛ New Project ➛Version Control ➛ Git.
Copy and paste the URL of your assignment repo into the dialog box Repository URL. Again, please make sure to have SSH highlighted under Clone when you copy the address.
Click Create Project, and the files from your GitHub repo will be displayed in the Files pane in RStudio.
Click lab-1.qmd to open the template Quarto file. This is where you will write up your code and narrative for the lab.
Step 3: Update the YAML
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 representation 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 (
.qmd
) file in your project, update theauthors
field to add your name first (first and last) and then your teammates’ names (first and last). Then, render the document.If you get a popup window error, click “Try again”.
Examine the rendered document and make sure your name is updated in the document.
Step 4: Commit your changes
-
Go to the Git pane in RStudio. This will be in the top right hand corner in a separate tab.
If you have made changes to your Quarto (.qmd) file, you should see it listed here. If you have rendered the document, you should also see its output, a PDF file, listed there.
-
Click on it to select it in this list and then click on Diff.
This shows you the difference between the last committed state of the document and its current state including changes. You should see deletions in red and additions in green.
-
If you’re happy with these changes, prepare the changes to be pushed to your remote repository.
First, stage your changes by checking the appropriate box on the files you want to prepare.
Next, write a meaningful commit message (for instance, “Updated author name”) in the Commit message box.
Finally, click Commit. Note that every commit needs to have a commit message associated with it.
You don’t have to commit after every change, as this would get quite tedious. You should commit states that are meaningful to you for inspection, comparison, or restoration (e.g., restoring a previous version of your document).
In the first few assignments, we will tell you exactly when to commit and, in some cases, what commit message to use. As the semester progresses, we will let you make these decisions.
Step 5: Push your changes
Now that you have made an update and committed this change, it’s time to push these changes to your repo on GitHub.
In the Git pane, click on Push.
Then, make sure all the changes went to GitHub. Go to your GitHub repo in your browser and refresh the page. You should see your commit message next to the updated files. If you see this, all your changes are on GitHub, and you’re good to go!
If you don’t see your update, go back to Step 4. Remember that in order to push your changes to GitHub, you must have staged (checked boxes) your commit (with a commit message) to be pushed and then click on Push.
Guidelines
Code
Code should follow the tidyverse style. Particularly,
- there should be spaces before and line breaks after each
+
when building aggplot
, - there should also be spaces before and line breaks after each
|>
in a data transformation pipeline, - code should be properly indented,
- there should be spaces around
=
signs and spaces after commas.
Additionally, all code should be visible in the PDF output, i.e., should not run off the page on the PDF. Long lines that run off the page should be split across multiple lines with line breaks.1
Plots
- Plots should have an informative title and, if needed, also a subtitle.
- Axes and legends should be labeled with both the variable name and its units (if applicable).
- Careful consideration should be given to aesthetic choices.
Workflow
Continuing to develop a sound workflow for reproducible data analysis is important as you complete the lab and other assignments in this course.
- You should have at least 3 commits with meaningful commit messages by the end of the assignment.
- Final versions of both your
.qmd
file and the rendered PDF should be pushed to GitHub.
Packages
In this lab we will work with the tidyverse package, which is a collection of packages for doing data analysis in a “tidy” way.
-
Run the code cell by clicking on the green triangle (play) button for the code cell labeled
load-packages
. This loads the package to make its features (the functions and datasets in it) be accessible from your Console. - Then, render the document which loads this package to make its features (the functions and datasets in it) be available for other code cells in your Quarto document.
Refresher: tidyverse
The tidyverse is a meta-package. When you load it you get nine packages loaded for you:
- dplyr: for data wrangling
- forcats: for dealing with factors
- ggplot2: for data visualization
- lubridate: for dealing with dates
- purrr: for iteration with functional programming
- readr: for reading and writing data
- stringr: for string manipulation
- tibble: for modern, tidy data frames
- tidyr: for data tidying and rectangling
Data
For this lab you will use a dataset on counties in North Carolina.
The dataset contains information on North Carolina counties retrieved from the 2020 Census as well as from myFutureNC Dashboard maintained by Carolina Demography at the University of North Carolina at Chapel Hill.
This dataset is stored in a file called nc-county.csv
in the data
folder of your project/repository.
You can read this file into R with the following code:
nc_county <- read_csv("data/nc-county.csv")
This will read the CSV (comma separated values) file from the data
folder and store the dataset as a data frame called nc_county
in R.
The variables in the dataset and their descriptions are as follows:
-
county
: Name of county. -
land_area_m2
: Land area of county in meters-squared, based on the 2020 census. -
land_area_mi2
: Land area of county in miles-squared, based on the 2020 census. -
pop_2020
: Population of county, based on the 2020 Census. -
pop_dens_2020
: Population density calculated as population (pop_2020
) divided by land area in miles-squared (people per mile-squared). -
county_type
: Peer county type classification based on population characteristics, socioeconomic status, and geographic features used for grouping counties with similar demographic, social, and economic characteristics, allowing them to be compared and benchmarked against one another. -
median_hh_income
: Median household income. -
p_foreign_born
: Percentage of population that is foreign-born. -
p_child_poverty
: Percentage of children living in poverty. -
p_single_parent_hh
: Percentage of households with children that are single-parent households. -
p_broadband
: Percentage of households with broadband internet access. -
p_home_ownership
: Percentage of households that are owner-occupied. -
p_family_sustaining_wage
: Percentage of adults that earn a family-sustaining wage – typically a wage that covers essential costs like housing, food, childcare, transportation, and healthcare for a family’s basic needs within a specific geographic area -
p_edu_lths
: Percentage of 25-44-year-olds with less than a high school diploma. -
p_edu_hsged
: Percentage of 25-44-year-olds with a high school diploma or equivalent. -
p_edu_scnd
: Percentage of 25-44-year-olds with some college or an associate degree. -
p_edu_ndc
: Percentage of 25-44-year-olds with non-degree credentials – certifications, licenses, or other credentials that demonstrate specific skills or knowledge but do not confer a formal academic degree. -
p_edu_assoc
: Percentage of 25-44-year-olds with an associate degree. -
p_edu_ba
: Percentage of 25-44-year-olds with a bachelor’s degree. -
p_edu_mapl
: Percentage of 25-44-year-olds with a master’s, professional, or doctoral degree. -
p_edu_hs_grad_rate
: High school graduation rate. -
p_edu_chronic_absent_rate
: Chronic absenteeism rate.
Questions
Question 1
- How many counties are in the dataset? How many variables are there? Use inline code to answer this question.
Render, commit, and push your changes to GitHub with the commit message “Added answer for Q1, part a”.
Make sure to commit and push all changed files so that your Git pane is empty afterward.
- Make two plots: a histogram and a boxplot of the
pop_2020
variable.
Is the distribution of population in NC counties right-skewed, left-skewed, or approximately symmetric? Which plot(s) helped you determine this?
Is the distribution of population in NC counties unimodal, bimodal, multimodal or uniform? Which plot(s) helped you determine this?
In a single pipeline, identify the counties with unusually high populations (according to the boxplot) and display their names and populations in descending order of population.
Render, commit, and push your changes to GitHub with the commit message “Added answer for Q1, part b”.
Make sure to commit and push all changed files so that your Git pane is empty afterward.
Question 2
- Guess what the relationship between population density and land area might be – positive? negative? no relationship? Explain your reasoning.
Render, commit, and push your changes to GitHub with a meaningful commit message.
Make sure to commit and push all changed files so that your Git pane is empty afterward.
- Make a scatter plot of population density (
density
on the y-axis) vs. land area in miles-squared (land_area_mi2
on the x-axis). Make sure to set an informative title and axis labels for your plot. Describe the relationship. Was your guess correct?
Now is another good time to render, commit, and push your all your changes to GitHub with a meaningful commit message.
- Now make a scatter plot of population density (
density
on the y-axis) vs. land area in meters-squared (land_area_m2
on the x-axis). Make sure to set an informative title and axis labels for your plot. Comment on how this scatterplot compares to the one in the previous part — is the relationship displayed same or different. Explain why.
Render, commit, and push your all your changes to GitHub with the commit message with a meaningful commit message.
Wrap-up
Before you wrap up the assignment, make sure that you render, commit, and push one final time so that the final versions of both your .qmd
file and the rendered PDF are pushed to GitHub and your Git pane is empty. We will be checking these to make sure you have been practicing how to commit and push changes.
Submission
Submit your PDF document to Gradescope by the end of the lab to be considered “on time”:
- Go to http://www.gradescope.com and click Log in in the top right corner.
- Click School Credentials \(\rightarrow\) Duke NetID and log in using your NetID credentials.
- Click on your STA 199 course.
- Click on the assignment, and you’ll be prompted to submit it.
- Mark all the pages associated with question. All the pages of your lab should be associated with at least one question (i.e., should be “checked”).
Make sure you have:
- attempted all questions
- rendered your Quarto document
- committed and pushed everything to your GitHub repository such that the Git pane in RStudio is empty
- uploaded your PDF to Gradescope
Grading and feedback
- This lab is worth 30 points:
- 10 points for being in lab and turning in something – no partial credit for this part.
- 20 points for:
- answering the questions correctly – there is partial credit for this part.
- following the workflow – there is partial credit for this part.
- The workflow points are for:
- committing at least three times as you work through your lab,
- having your final version of
.qmd
and.pdf
files in your GitHub repository, and - overall organization.
- You’ll receive feedback on your lab on Gradescope within a week.
Good luck, and have fun with it!
Footnotes
Remember, haikus not novellas when writing code!↩︎