Get access to all lessons in this course.
-
Advanced Data Wrangling
- Downloading and Importing Data
- Overview of Tidy Data
- Tidy Data Rule #1: Every Column is a Variable
- Tidy Data Rule #3: Every Cell is a Single Value
- Tidy Data Rule #2: Every Row is an Observation
- Changing Variable Types
- Dealing with Missing Data
- Advanced Summarizing
- Binding Data Frames
- Functions
- Data Merging
- Exporting Data
- Bring It All Together (Advanced Data Wrangling)
-
Advanced Data Visualization
- Best Practices in Data Visualization
- Tidy Data
- Pipe Data into ggplot
- Reorder Plots to Highlight Findings
- Line Charts
- Use Color to Highlight Findings
- Declutter
- Add Descriptive Labels to Your Plots
- Use Titles to Highlight Findings
- Use Annotations to Explain
- Tweak Spacing
- Create a Custom Theme
- Customize Your Fonts
- Try New Plot Types
- Bring it All Together (Advanced Data Visualization)
-
Quarto
- Advanced Markdown
- Advanced YAML and Code Chunk Options
- Tables
- Inline R Code
- Making Your Reports Shine: Word Edition
- Making Your Reports Shine: PDF Edition
- Making Your Reports Shine: HTML Edition
- Presentations
- Dashboards
- Websites
- Publishing Your Work
- Quarto Extensions
- Parameterized Reporting, Part 1
- Parameterized Reporting, Part 2
- Parameterized Reporting, Part 3
- Wrapping up Going Deeper with R
Going Deeper with R
Quick Interlude to Reorganize our Code
This lesson is locked
This lesson is called Quick Interlude to Reorganize our Code, part of the Going Deeper with R course. This lesson is called Quick Interlude to Reorganize our Code, part of the Going Deeper with R course.
If the video is not playing correctly, you can watch it in a new window
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Your Turn
Reorganize your code so that you only create the enrollment_by_race_ethnicity
data frame in one place.
Learn More
I haven’t found many resources that give recommendations for organizing code. I think it’s a) idiosyncratic to individuals, and b) the kind of thing that people who have used R for a while do without even thinking about it. The one resource I’ve found is called R Best Practices by Krista DeStasio.
My general practice is this:
Load packages at the top of my files. This ensures that you have access to all functions throughout your files.
Only create objects once. This avoids the issue we encountered where you don’t know what state your object is in.
Create as few objects as possible. I’ve found that by doing all of my data cleaning and tidying before beginning analysis enables me to create just a few objects, which I can then easily manipulate with a few lines of code to show a wide range of results.
You need to be signed-in to comment on this post. Login.
Matt M
December 3, 2021
you said that you don't "need" to include enrollment_by_race_ethnicity as the x in left_join() but when I try to include it, the code does not run. as soon as I deleted it, it runs. Is that we cannot include it here because it has not been created yet?
Jordan Helms
May 10, 2022
I ran into this issue during the Functions lesson. When I try to run the code, even copy what's in the solution, I get this error message:
"Error in
mutate()
: ! Problem while computingnumber_of_students = replace_na(number_of_students, 0)
. Caused by error invec_assign()
: ! Can't convertreplace
to match type ofdata
."It happens with the 2018-2019 data. This section: "enrollment_by_race_ethnicity_18_19 <- clean_enrollment_data(raw_data = enrollment_18_19, data_year = "2018-2019", race_ethnicity_remove_text = "x2018_19_")"
I can run the code with no problems with the 2017-2018 data. When I don't use the function and do two separate code chunks, I don't get the error message. Unsure what's going on.
Niger Sultana
May 17, 2022
Hi I know David sent us the solution of debugging (replace _na), I cannot find the solution about code below, sorry probably lost the e-mail, could you please help me to show where is this to solve the code?
clean_enrollment_data %
Error in mutate(number_of_students = as.numeric(number_of_students)) : object 'number_of_students' not found