Get access to all lessons in this course.
-
Advanced Data Wrangling and Analysis
- Overview
- Importing Data
- Tidy Data
- Reshaping Data
- Dealing with Missing Data
- Changing Variable Types
- Advanced Variable Creation
- Advanced Summarizing
- Binding Data Frames
- Functions
- Merging Data
- Renaming Variables
- Quick Interlude to Reorganize our Code
- Exporting Data
-
Advanced Data Visualization
- Data Visualization Best Practices
- Tidy Data
- Pipe Data Into ggplot
- Reorder Plots to Highlight Findings
- Line Charts
- Use Color to Highlight Findings
- Declutter
- Use the scales Package for Nicely Formatted Values
- Use Direct Labeling
- Use Axis Text Wisely
- Use Titles to Highlight Findings
- Use Color in Titles to Highlight Findings
- Use Annotations to Explain
- Tweak Spacing
- Customize Your Theme
- Customize Your Fonts
- Try New Plot Types
-
Advanced RMarkdown
- Advanced Markdown Text Formatting
- Tables
- Advanced YAML
- Inline R Code
- Making Your Reports Shine: Word Edition
- Making Your Reports Shine: HTML Edition
- Making Your Reports Shine: PDF Edition
- Presentations
- Dashboards
- Other Formats
-
Wrapping Up
- You Did It!
Going Deeper with R (v1)
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 (v1) course. This lesson is called Quick Interlude to Reorganize our Code, part of the Going Deeper with R (v1) 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