This notebook documents part of a data analysis I completed in the capstone experience for the Google Data Analytics Professional Certificate program on Coursera.
You can find the Github repository for my work here.
This phase involves cleaning and transforming data, maintaining data integrity, testing data and verifying and reporting on cleaning results.
I am using libraries from the tidyverse collection of R packages and supplementing with the knitr library to format tables contained within this notebook.
packages <- c("dplyr", "ggplot2", "knitr", "lubridate", "stringr", "tidyr")
installed_packages <- packages %in% rownames(installed.packages())
if(any(installed_packages == FALSE)) {
install.packages(packages[!installed_packages])
}
invisible(lapply(packages, library, character.only = TRUE))
The code chunk below loads the source data gathered in the prepare phase of this project.
load("C:/R/Packages/cyclistic/data/source.rda")
trip <- source
The data frame contains 13 columns and 5,667,717 rows of data.
I’ll start by checking for missing values.
The code below counts the number of missing values by column and presents the counts in a long-form table.
missing_values <- trip %>%
summarise_all(funs(sum(is.na(.)))) %>%
pivot_longer(cols = everything()) %>%
rename(column_name = name,
row_count = value)
## Warning: `funs()` was deprecated in dplyr 0.8.0.
## ℹ Please use a list of either functions or lambdas:
##
## # Simple named list: list(mean = mean, median = median)
##
## # Auto named with `tibble::lst()`: tibble::lst(mean, median)
##
## # Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
kable(missing_values,
col.names = c("Column Name", "Row Count"),
caption = "Missing Values by Column",
format.args = list(big.mark = ","),
align = c("l", "r"))
| Column Name | Row Count |
|---|---|
| ride_id | 0 |
| rideable_type | 0 |
| started_at | 0 |
| ended_at | 0 |
| start_station_name | 833,064 |
| start_station_id | 833,064 |
| end_station_name | 892,742 |
| end_station_id | 892,742 |
| start_lat | 0 |
| start_lng | 0 |
| end_lat | 5,858 |
| end_lng | 5,858 |
| member_casual | 0 |
There are rows with missing location data, including station name, id and location coordinates. Since the missing data can’t be imputed, I will omit these rows from the data frame.
trip <- trip %>%
na.omit()
Dropping these rows reduces the overall number of rows in the source data to 4,369,360 rows for analysis.
To facilitate additional cleaning, will format columns in the source data frame and ensure there are no duplicate rows in the data frame.
The code chunk below makes the following updates to the data frame:
trip <- trip %>%
rename(ride_type = rideable_type,
start_date_time = started_at,
end_date_time = ended_at,
start_latitude = start_lat,
start_longitude = start_lng,
end_latitude = end_lat,
end_longitude = end_lng,
member_type = member_casual) %>%
mutate(start_latitude = round(start_latitude, 5),
start_longitude = round(start_longitude, 5),
end_latitude = round(end_latitude, 5),
end_longitude = round(end_longitude, 5),
start_date_time = lubridate::ymd_hms(start_date_time),
end_date_time = lubridate::ymd_hms(end_date_time),
trip_duration_in_minutes = round(as.numeric((end_date_time - start_date_time) / 60), 2),
trip_type = if_else(start_station_name == end_station_name, "Loop", "Point-to-Point"),
start_hour_of_day = lubridate::hour(start_date_time),
end_hour_of_day = lubridate::hour(end_date_time),
ride_type = case_when(ride_type == "classic_bike" ~ "Classic",
ride_type == "docked_bike" ~ "Docked",
ride_type == "electric_bike" ~ "Electric"),
member_type = case_when(member_type == "casual" ~ "Casual",
member_type == "member" ~ "Member"),
day_of_week = lubridate::wday(start_date_time, TRUE, TRUE),
category_of_day = if_else(day_of_week %in% c("Sat", "Sun"), "Weekend", "Workweek"),
start_end_category = if_else(start_hour_of_day == end_hour_of_day,
"Same",
"Different"))
I’m going to filter the data frame to include only those rows that are same-day trips since that’s the most common scenario in this data.
trip <- trip %>%
filter(str_sub(start_date_time, 1, 10) == str_sub(end_date_time, 1, 10))
There are some rows with a trip duration of zero or negative values. There are also rows with durations of close to 24 hours. I will filter the data frame to include trips with a duration in the range of 5-960 minutes, i.e. eight hours.
trip <- trip %>%
filter(trip_duration_in_minutes >= 5 & trip_duration_in_minutes <= 960)
This leaves a range of duration values from 5 to 959.58 minutes.
Some station names are causing duplicates. The following code addresses them by cleaning the station names.
trip <- trip %>%
mutate(start_station_name = str_replace(start_station_name, "Public Rack - ", "")) %>%
mutate(end_station_name = str_replace(end_station_name, "Public Rack - ", "")) %>%
mutate(start_station_name = str_replace(start_station_name, "Public Rack - ", "")) %>%
mutate(end_station_name = str_replace(end_station_name, "Public Rack - ", "")) %>%
mutate(start_station_name = str_replace(start_station_name, "Pubic Rack - ", "")) %>%
mutate(end_station_name = str_replace(end_station_name, "Pubic Rack - ", "")) %>%
mutate(start_station_name = str_replace(start_station_name, "City Rack - ", "")) %>%
mutate(end_station_name = str_replace(end_station_name, "City Rack - ", "")) %>%
mutate(start_station_name = str_replace(start_station_name, " - midblock", "")) %>%
mutate(end_station_name = str_replace(end_station_name, " - midblock", "")) %>%
mutate(start_station_name = str_replace(start_station_name, " - midblock south", "")) %>%
mutate(end_station_name = str_replace(end_station_name, " - midblock south", "")) %>%
mutate(start_station_name = str_replace(start_station_name, " - Charging", "")) %>%
mutate(end_station_name = str_replace(end_station_name, " - Charging", "")) %>%
mutate(start_station_name = str_replace(start_station_name, " (Temp)", "")) %>%
mutate(end_station_name = str_replace(end_station_name, " (Temp)", ""))
And the following code addresses variants of the same station names.
trip <- trip %>%
mutate(start_station_name = str_replace(start_station_name,
"N Shore Channel Trail & Argyle Ave",
"N Shore Channel Trail & Argyle St")) %>%
mutate(end_station_name = str_replace(end_station_name,
"N Shore Channel Trail & Argyle Ave",
"N Shore Channel Trail & Argyle St")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Ewing Ave & 106th St",
"Ewing Ave & 106th St NW")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Ewing Ave & 106th St",
"Ewing Ave & 106th St NW")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Ewing Ave & 96th St",
"Ewing Ave & 96th St N")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Ewing Ave & 96th St",
"Ewing Ave & 96th St N")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Mt Greenwood Library - North",
"Mt Greenwood Library N")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Mt Greenwood Library - North",
"Mt Greenwood Library N")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Kedzie & 103rd St - West",
"Kedzie & 103rd St - W")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Kedzie & 103rd St - West",
"Kedzie & 103rd St - W")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Lafayette Ave & 87th St SW",
"Lafayette Ave & 87th St - SW")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Lafayette Ave & 87th St SW",
"Lafayette Ave & 87th St - SW")) %>%
mutate(start_station_name = str_replace(start_station_name,
"63rd & Western Ave - south corner",
"63rd & Western Ave S")) %>%
mutate(end_station_name = str_replace(end_station_name,
"63rd & Western Ave - south corner",
"63rd & Western Ave S")) %>%
mutate(start_station_name = str_replace(start_station_name,
"63rd & Western Ave - north corner",
"63rd & Western Ave N")) %>%
mutate(end_station_name = str_replace(end_station_name,
"63rd & Western Ave - north corner",
"63rd & Western Ave N")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Kedzie Ave & 61st Pl",
"Kedzie Ave & 61st Pl E")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Kedzie Ave & 61st Pl",
"Kedzie Ave & 61st Pl E")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Prairie Ave & 47th St ",
"Prairie Ave & 47th St")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Prairie Ave & 47th St ",
"Prairie Ave & 47th St")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Woodlawn Ave & 63rd St - SE",
"Woodlawn Ave & 63rd St S")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Woodlawn Ave & 63rd St - SE",
"Woodlawn Ave & 63rd St S")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Woodlawn Ave & 63rd St - NE",
"Woodlawn Ave & 63rd St N")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Woodlawn Ave & 63rd St - NE",
"Woodlawn Ave & 63rd St N")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Prairie Ave & Garfield Blvd",
"Prairie Ave & Garfield Blvd N")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Prairie Ave & Garfield Blvd",
"Prairie Ave & Garfield Blvd N")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Kedvale Ave & 63rd St",
"Kedvale Ave & 63rd St W")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Kedvale Ave & 63rd St",
"Kedvale Ave & 63rd St W")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Kedzie Ave & 62nd Pl",
"Kedzie Ave & 62nd Pl")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Kedzie Ave & 62nd Pl",
"Kedzie Ave & 62nd Pl")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Kedzie Ave & 61st Pl",
"Kedzie Ave & 61st Pl W")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Kedzie Ave & 61st Pl",
"Kedzie Ave & 61st Pl W")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Bissell St & Armitage Ave*",
"Bissell St & Armitage Ave")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Bissell St & Armitage Ave*",
"Bissell St & Armitage Ave")) %>%
mutate(start_station_name = str_replace(start_station_name,
"WestChi",
"WEST CHI-WATSON")) %>%
mutate(end_station_name = str_replace(end_station_name,
"WestChi",
"WEST CHI-WATSON")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Base - 2132 W Hubbard",
"Base - 2132 W Hubbard Warehouse")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Base - 2132 W Hubbard",
"Base - 2132 W Hubbard Warehouse")) %>%
mutate(start_station_name = str_replace(start_station_name,
"Base - 2132 W Hubbard",
"Base - 2132 W Hubbard Warehouse")) %>%
mutate(end_station_name = str_replace(end_station_name,
"Base - 2132 W Hubbard",
"Base - 2132 W Hubbard Warehouse"))
Drop rows that have zero values for latitude and/or longitude.
trip <- trip %>%
filter(start_latitude != 0) %>%
filter(start_longitude != 0) %>%
filter(end_latitude != 0) %>%
filter(end_longitude != 0)
Ensure data frame has distinct rows.
trip <- trip %>%
distinct_all()
Filter the data to exclude extreme values in trip duration that lie outside the calculated interquartile range for this data field. Dropping these extreme values will aid exploratory analysis by making patterns related to trip duration more apparent.
Calculate and use the interquartile range to identify and exclude extreme values.
quartiles <- quantile(trip$trip_duration_in_minutes,
probs = c(.25, .75),
na.rm = FALSE)
iqr <- IQR(trip$trip_duration_in_minutes)
lower <- quartiles[1] - 1.5 * iqr
upper <- quartiles[2] + 1.5 * iqr
trip <- trip %>%
filter(trip_duration_in_minutes > lower & trip_duration_in_minutes < upper)
Set seed for reproducibility and select a random sample of 250,000 rows to facilitate an exploratory analysis and avoid long code run times.
set.seed(91210)
trip <- trip %>%
slice_sample(n = 250000)
Save the cleaned trip data frame. It now contains 20 columns and 250,000 rows of data.
write.csv(trip,
"../inst/extdata/output/trip.csv",
row.names = FALSE)
usethis::use_data(trip, overwrite = TRUE)
## ✔ Setting active project to 'C:/R/Packages/cyclistic'
## ✔ Saving 'trip' to 'data/trip.rda'
## • Document your data (see 'https://r-pkgs.org/data.html')
Create a calendar table.
start_date <- as.Date("2022-01-01")
end_date <- as.Date("2022-12-31")
calendar <- data.frame(date = seq.Date(start_date, end_date, by = "1 day"))
calendar <- calendar %>%
mutate(year = lubridate::year(date),
quarter = lubridate::quarter(date),
month = lubridate::month(date),
week = lubridate::week(date),
day_of_month = lubridate::day(date),
year_and_quarter = paste(lubridate::year(date),
"-",
str_pad(lubridate::quarter(date), 2, "left", "0"),
sep = ""),
year_and_month = paste(lubridate::year(date),
"-",
str_pad(lubridate::month(date), 2, "left", "0"),
sep = ""),
month_long_name = lubridate::month(date, TRUE, FALSE),
month_short_name = lubridate::month(date, TRUE, TRUE),
day = lubridate::wday(date),
day_long_name = lubridate::wday(date, TRUE, FALSE),
day_short_name = lubridate::wday(date, TRUE, TRUE))
write.csv(calendar,
"../inst/extdata/output/calendar.csv",
row.names = FALSE)
usethis::use_data(calendar, overwrite = TRUE)
## ✔ Saving 'calendar' to 'data/calendar.rda'
## • Document your data (see 'https://r-pkgs.org/data.html')
Create a destination table.
Purpose is to compare trips to popular destinations in the city.
destination <- read.delim("../inst/extdata/source/destination.txt",
header = TRUE,
sep = ";",
na.strings = c(""),
stringsAsFactors = FALSE)
write.csv(destination,
"../inst/extdata/output/destination.csv",
row.names = FALSE)
usethis::use_data(destination, overwrite = TRUE)
## ✔ Saving 'destination' to 'data/destination.rda'
## • Document your data (see 'https://r-pkgs.org/data.html')
Regarding data integrity, I noted the following considerations:
The next step in this project is to analyze the data in preparation for sharing insights and recommendations.