Data Wrangling - Tasks
In this section, we will practice data wrangling using R and the dplyr package from the tidyverse. Follow the steps below to manipulate and explore a dataset.
Create a new R script in your
scriptsfolder and name it2-data-wrangling-and-visualization.R.Load the
tidyversepackage by adding the following line at the top of your script:library(tidyverse)Download the
crashesfile (here) and save it in yourdatafolder.Read the
crashesdataset into R using theread_csv()function and assign it to a variable namedcrashes:crashes <- read_csv("data/crashes.csv")Explore the dataset:
- Use the
head()function to view the first few rows of the dataset. - Use the
str()function to understand the structure of the dataset. - Use the
summary()function to get a summary of the dataset.
- Use the
Data Wrangling Tasks:
- Create a new data.frame named
crashes_filteredthat includes only cyclists. - Create a new data.frame named
crashes_darkthat includes only crashes that occurred in dark conditions. - Create a new data.frame named
crashes_dark_cyclistthat includes only crashes that involved cyclists and occurred in dark conditions. - Create a summary table named
crashes_by_typethat shows the median age by casualty type.
- Create a new data.frame named
Reuse
Copyright
© 2025 Robin Lovelace & contributors