Get access to all lessons in this course.
-
Week 1: Getting Started with R
- Welcome to Getting Started with R
- Install R
- Install RStudio
- Projects
- Files in R
- Packages
- Import Data
- Objects and Functions
- Examine our Data
- Import Our Data Again
- Getting Help
- Wrapping Up
- R in 3 Months Spring 2022 Week 1 Live Session
-
Week 2: Fundamentals of R (RMarkdown)
- Welcome to Fundamentals of R
- RMarkdown Overview
- YAML
- Text
- Code Chunks
- Wrapping Up
- R in 3 Months Spring 2022 Week 2 Project Assignment
- R in 3 Months Spring 2022 Week 2 Office Hours
- R in 3 Months Spring 2022 Week 2 Live Session
-
Week 3: Fundamentals of R (Data Wrangling and Analysis)
- Getting Started
- The Tidyverse
- select
- mutate
- filter
- summarize
- group_by
- count
- arrange
- Create a New Data Frame
- Crosstabs
- Wrapping Up
- R in 3 Months Spring 2022 Week 3 Office Hours
- R in 3 Months Spring 2022 Week 3 Live Session
- R in 3 Months Spring 2022 Week 3 Project Assignment
-
Week 4: Fundamentals of R (Data Visualization)
- An Important Workflow Tip
- The Grammar of Graphics
- Scatterplots
- Histograms
- Bar Charts
- color and fill
- scales
- Text and Labels
- Plot Labels
- Themes
- Facets
- Save Plots
- Wrapping Up
- You Did It!
- R in 3 Months Spring 2022 Week 4 Office Hours
- R in 3 Months Spring 2022 Week 4 Live Session
- R in 3 Months Spring 2022 Week 4 Project Assignment
-
Week 5: Catch-Up Week
- R in 3 Months Spring 2022 Week 5 Office Hours
- R in 3 Months Spring 2022 Week 5 Project Assignment: ASSIGNMENT AMNESTY
-
Week 6: Git + GitHub
- What is Git? What is GitHub?
- Why Should You Learn to Use Git and GitHub?
- Update Everything
- Install Git
- Configure Git
- Create a Local Git Repository
- Commits
- Commit History
- GitHub Repositories
- Connect RStudio and GitHub
- Push an RStudio Project to a GitHub Repository
- Pull a GitHub Repository to an RStudio Project
- Keep RStudio and GitHub in Sync
- R in 3 Months Spring 2022 Week 6 Office Hours
- R in 3 Months Spring 2022 Week 6 Live Session
- R in 3 Months Spring 2022 Week 6 Project Assignment
-
Week 7: Going Deeper with R (Advanced Data Wrangling, Part 1)
- Overview
- Importing Data
- Tidy Data
- Reshaping Data
- Dealing with Missing Data
- Changing Variable Types
- Advanced Variable Creation
- Advanced Summarizing
- Binding Data Frames
- R in 3 Months Spring 2022 Week 7 Office Hours
- R in 3 Months Spring 2022 Week 7 Project Assignment
- R in 3 Months Spring 2022 Week 7 Live Session
-
Week 8: Going Deeper with R (Advanced Data Wrangling, Part 2)
- Functions
- Merging Data
- Renaming Variables
- Quick Interlude to Reorganize our Code
- Exporting Data
- R in 3 Months Spring 2022 Week 8 Office Hours
- R in 3 Months Spring 2022 Week 8 Live Session
- R in 3 Months Spring 2022 Week 8 Project Assignment
-
Week 9: Catch-Up Week
- R in 3 Months Spring 2022 Week 9 Office Hours
- R in 3 Months Spring 2022 Week 9 - Assignment Amnesty
-
Week 10: Going Deeper with R (Advanced Data Visualization, Part 1)
- 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
- R in 3 Months Spring 2022 Week 10 Office Hours
- R in 3 Months Spring 2022 Week 10 Live Session
- R in 3 Months Spring 2022 Week 10 Project Assignment
-
Week 11: Going Deeper with R (Advanced Data Visualization, Part 2)
- 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
- R in 3 Months Spring 2022 Week 11 Live Session
- R in 3 Months Spring 2022 Week 11 Office Hours
- R in 3 Months Spring 2022 Week 11 Project Assignment
-
Week 12: Going Deeper with R (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
- You Did It!
- R in 3 Months Spring 2022 Week 12 Office Hours
- R in 3 Months Spring 2022 Week 12 Live Session
- R in 3 Months Spring 2022 Week 12 Project Assignment
-
Week 13: Final Assignment
- R in 3 Months Spring 2022 Week 13 Office Hours
- R in 3 Months Spring 2022 Week 13 Live Session
- R in 3 Months Spring 2022 Final Project Assignment
-
WEEK 14: Retrospective
R in 3 Months (Spring 2022)
Reshaping Data
This lesson is locked
This lesson is called Reshaping Data, part of the R in 3 Months (Spring 2022) course. This lesson is called Reshaping Data, part of the R in 3 Months (Spring 2022) 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
Start with the
enrollment_18_19
data frameselect()
thedistrict_id
variable as well as those about number of students by race/ethnicity and get rid of all others (hint: use thecontains()
helper function withinselect()
)Use
pivot_longer()
to convert all of the race/ethnicity variables into one variableWithin
pivot_longer()
, use the names_to argument to call that variablerace_ethnicity
Within
pivot_longer()
, use the values_to argument to call that variablenumber_of_students
Learn More
The best place to learn more about pivot_longer()
and pivot_wider()
is the pivoting vignette from the tidyr
package.
There’s also a nice article by Gavin Simpson of University College, London about pivoting. That article includes the animations below, made by Garrick Aden-Buie and Mara Averick, that gave a visual demonstration of pivoting.
RStudio has a nice primer on reshaping data, complex with a few exercises.
Finally, a heads up: if you ever see references to the functions gather()
and spread()
, these are the previous iterations of the pivot
functions. They still work (as the tweet below from tidyverse developer Hadley Wickham indicates), but the pivot
functions are, in my view (and the view of many others), much easier to use.
You may have heard a rumour that gather/spread are going away. This is simply not true (they’ll stay around forever) but I am working on better replacements which you can learn about at https://t.co/sU2GzWeBaf. Now is a great time for feedback! #rstats
— Hadley Wickham (@hadleywickham) March 19, 2019
You need to be signed-in to comment on this post. Login.
IBRAH SENINDE
April 22, 2021
Hi David, I typed the following code, but the new data frame still the original structure. What could be the problem? enrollment_by_race_ethnicity_18_19 % select(-contains("grade")) %>% select(-contains("kindergarten")) %>% select(-contains("percent")) %>% pivot_longer(cols = "district_id", names_to = "race_ethnicity", values_to = "number_of_students")
Harold Stanislaw
April 22, 2021
I used select(!contains ("percent")) instead of select(-contains ("percent")), mainly because the helper page listed the exclamation option rather than the minus sign. Are there any differences between the two?
Matt M
November 9, 2021
I only have enrollment_17_18 and enrollment_18_19 files. Where did the math scores files come from that is shown at 5:40 of this lesson? My code matches what's in the Solutions for the Importing Data lesson and I don't think there was anything we downloaded in the Tidy Data lesson.
Alberto Espinoza
September 10, 2022
Hi David, is there a way to enter the name of each column we want to remove within one select(-contains(" ")) argument rather than writing each one separately?
Nadia Chang
November 24, 2022
Hi David! by using pivot_longer it means that it will always arrange the data by 3 columns?