Introduction: Google Capstone Project Case Study 1

This case study presents a capstone project focused on Cyclistic, a bike-share company. The primary objective is to analyze the usage patterns of bike users (both casual and members) to derive insights for strategizing the company’s upcoming marketing campaign

Background Details

This is an analysis examining the distinctive usage patterns between casual users and members of Cyclistic, a bike-share company. Cyclistic operates with approximately 5,824 geotracked bicycles across a network of 692 stations throughout Chicago. These bikes are accessible from one station and can be returned to any other station within the system at any given time.This analysis is based on the Divvy case study “‘Sophisticated, Clear, and Polished’ of Oct 2020 to September 2021: Divvy and Data Visualization” written by Kevin Hartman (found here:https://artscience.blog/home/divvy-dataviz-case-study).

Data Source

I’m utilizing datasets with altered names since Cyclistic is a fictitious company. These datasets, tailored for this case study, are suitable for addressing the business queries effectively. The data has been provided by Motivate International Inc. under a specific license.

Business Task

My objective is to discern the distinctive usage patterns between annual members and casual riders of Cyclistic bikes. This analysis will aid in crafting marketing strategies focused on converting casual riders into annual members.

Stakeholders

SET UP OUR ENVIRONMENT

I have a couple of options here - but i will need to load the essential packages needed

The following packages are essential for effectiveness of this analysis

library(tidyverse) #helps wrangle data library(lubridate) #helps wrangle date attributes library(ggplot2) #helps visualize data library(skimr) #For summerizing data library(janitor) #For cleaning data library(readr) #to provide a fast and friendly way to read rectangular data library(scales) #to get percent() function library(ggthemes) #complete themes which control all non-data display library(data.table) #aggregation of large datasets library(dplyr) #for data manipulation library(stringr) #provide string handling capabilities library(DT) #to return the probability density analysis library(tidyr) #creating tidy data library(corrplot) #to plot the graph of the correlation matrix library(leaflet) #to create interactive map

To display working directory

getwd()

To set working directory so as to simplify calls to data

setwd(“/Users/miftaaudeenjamiu/Desktop/divy_data”)

Upload all the divy datasets

q2_2019 <- read_csv (“Divvy_Trips_2019_Q2.csv”) q3_2019 <- read_csv (“Divvy_Trips_2019_Q3.csv”) q4_2019 <- read_csv (“Divvy_Trips_2019_Q4.csv”) q1_2020 <- read_csv (“Divvy_Trips_2020_Q1.csv”)

STEP 2: WRANGLE DATA AND COMBINE INTO A SINGLE FILE

While the names don’t have to be in the same order, they DO need to match perfectly before we can use a command to join them into one file

colnames(q3_2019) colnames(q4_2019) colnames(q2_2019) colnames(q1_2020)

Rename columns to make them consistent with q1_2020 (as this will be the supposed going-forward table design for Divvy)

(q4_2019 <- rename(q4_2019 ,ride_id = trip_id ,rideable_type = bikeid ,started_at = start_time
,ended_at = end_time
,start_station_name = from_station_name ,start_station_id = from_station_id ,end_station_name = to_station_name ,end_station_id = to_station_id ,member_casual = usertype))

(q3_2019 <- rename(q3_2019 ,ride_id = trip_id ,rideable_type = bikeid ,started_at = start_time
,ended_at = end_time
,start_station_name = from_station_name ,start_station_id = from_station_id ,end_station_name = to_station_name ,end_station_id = to_station_id ,member_casual = usertype))

(q2_2019 <- rename(q2_2019 ,ride_id = “01 - Rental Details Rental ID” ,rideable_type = “01 - Rental Details Bike ID” ,started_at = “01 - Rental Details Local Start Time”
,ended_at = “01 - Rental Details Local End Time”
,start_station_name = “03 - Rental Start Station Name” ,start_station_id = “03 - Rental Start Station ID” ,end_station_name = “02 - Rental End Station Name” ,end_station_id = “02 - Rental End Station ID” ,member_casual = “User Type”))

Inspect the dataframes and look for incongruencies

str(q1_2020) str(q4_2019) str(q3_2019) str(q2_2019)

Convert ride_id and rideable_type to character so that they can stack correctly

q4_2019 <- mutate(q4_2019, ride_id = as.character(ride_id) ,rideable_type = as.character(rideable_type)) q3_2019 <- mutate(q3_2019, ride_id = as.character(ride_id) ,rideable_type = as.character(rideable_type)) q2_2019 <- mutate(q2_2019, ride_id = as.character(ride_id) ,rideable_type = as.character(rideable_type))

Inspect the dataframes and look for incongruencies

str(q1_2020) str(q4_2019) str(q3_2019) str(q2_2019)

Stack individual quarter’s data frames into one big data frame

all_trip <- bind_rows(q2_2019, q3_2019, q4_2019, q1_2020)

Remove lat, long, birthyear, and gender fields as this data was dropped beginning in 2020

all_trip <- all_trip %>%
select(-c(start_lat, start_lng, end_lat, end_lng, birthyear, gender, “01 - Rental Details Duration In Seconds Uncapped”, “05 - Member Details Member Birthday Year”, “Member Gender”, “tripduration”))

Show all_trip

head(all_trip) glimpse(all_trip)

STEP 3: CLEAN UP AND ADD DATA TO PREPARE FOR ANALYSIS

Inspect the new table that has been created

colnames(all_trip) #List of column names nrow(all_trip) #How many rows are in data frame? dim(all_trip) #Dimensions of the data frame? head(all_trip) #See the first 6 rows of data frame. Also tail(all_trips) str(all_trip) #See list of columns and data types (numeric, character, etc) summary(all_trip) #Statistical summary of data. Mainly for numerics

There are a few problems we will need to fix:
(1) In the “member_casual” column, there are two names for members (“member” and “Subscriber”) and two names for casual riders (“Customer” and “casual”). We will need to consolidate that from four to two labels.
(2) The data can only be aggregated at the ride-level, which is too granular. We will want to add some additional columns of data – such as day, month, year – that provide additional opportunities to aggregate the data.
(3) We will want to add a calculated field for length of ride since the 2020Q1 data did not have the “tripduration” column. We will add “ride_length” to the entire dataframe for consistency.
(4) There are some rides where tripduration shows up as negative, including several hundred rides where Divvy took bikes out of circulation for Quality Control reasons. We will want to delete these rides.
In the “member_casual” column, replace “Subscriber” with “member” and “Customer” with “casual”
Before 2020, Divvy used different labels for these two types of riders … we will want to make our dataframe consistent with their current nomenclature.
N.B.: “Level” is a special property of a column that is retained even if a subset does not contain any values from a specific level
Begin by seeing how many observations fall under each usertype

table(all_trip$member_casual)

Reassign to the desired values (we will go with the current 2020 labels)

all_trip <- all_trip %>% mutate(member_casual = recode(member_casual ,“Subscriber” = “member” ,“Customer” = “casual”))

Check to make sure the proper number of observations were reassigned

table(all_trip$member_casual)

Add columns that list the date, month, day, and year of each ride
This will allow us to aggregate ride data for each month, day, or year … before completing these operations we could only aggregate at the ride level

all_trip\(date <- as.Date(all_trip\)started_at) #The default format is yyyy-mm-dd

all_trip\(month <- format(as.Date(all_trip\)date), “%m”)

all_trip\(day <- format(as.Date(all_trip\)date), “%d”)

all_trip\(year <- format(as.Date(all_trip\)date), “%Y”)

all_trip\(day_of_week <- format(as.Date(all_trip\)date), “%A”)

We need to convert started_at and ended_at to POSIXct from Char Format

all_trip\(ended_at <- as.POSIXct(all_trip\)ended_at, format = “%Y-%m-%d %H:%M:%S”)

all_trip\(started_at <- as.POSIXct(all_trip\)started_at, format = “%Y-%m-%d %H:%M:%S”)

Add a “ride_length” calculation to all_trips (in seconds)

all_trip\(ride_length <- difftime(all_trip\)ended_at,all_trip$started_at)

Inspect the structure of the columns

str(all_trip)

Convert “ride_length” from Factor to numeric so we can run calculations on the data

is.factor(all_trip$ride_length)

all_trip\(ride_length <- as.numeric(as.character(all_trip\)ride_length))

To confirm the conversion

is.numeric(all_trip$ride_length)

Remove “bad” data
The dataframe includes a few hundred entries when bikes were taken out of docks and checked for quality by Divvy or ride_length was negative
We will create a new version of the dataframe (v2) since data is being removed

all_trips_v2 <- all_trip[!(all_trip\(start_station_name == "HQ QR" | all_trip\)ride_length<0),]

The NAs!

If we don’t handle our NA values we’re going to be in for a bad time.

We need to remove ridelength with attributes of “NA”

all_trips_v2 <- all_trips_v2 %>% filter(ride_length != “NA”,)

head(all_trips_v2)

STEP 4 CONDUCT DESCRIPTIVE ANALYSIS

Descriptive analysis on ride_length (all figures in seconds)

mean(all_trips_v2$ride_length) #straight average (total ride length / rides)

median(all_trips_v2$ride_length) #midpoint number in the ascending array of ride lengths

max(all_trips_v2$ride_length) #longest ride

min(all_trips_v2$ride_length) #shortest ride

See the average ride time by each day for members vs casual users

aggregate(all_trips_v2\(ride_length ~ all_trips_v2\)member_casual + all_trips_v2$day_of_week, FUN = mean)

Notice that the days of the week are out of order. Let’s fix that.

all_trips_v2\(day_of_week <- ordered(all_trips_v2\)day_of_week, levels=c(“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”))

Now, let’s run the average ride time by each day for members vs casual users

aggregate(all_trips_v2\(ride_length ~ all_trips_v2\)member_casual + all_trips_v2$day_of_week, FUN = mean)

Lets create a column to calculate days of the week named weekday

all_trips_v2 <- all_trips_v2 %>% mutate(weekday =format(as.Date(started_at), “%A”))

Analyze ridership data by type and weekday

cleaned_data <- all_trips_v2 %>% group_by ( member_casual, weekday) %>% summarise(Average_duration = mean(ride_length), Number_of_rides = n()) %>% arrange(member_casual,weekday)

head(cleaned_data)

STEP 5 : VISUALIZATION

Let’s create a visualization for Average duration Vs Bike Usertype

ggplot(data = cleaned_data, aes(x = weekday, y = Average_duration, fill = member_casual)) + geom_col(position = “dodge”) + scale_y_continuous() + labs(title = ‘Weekday vs Average Duration by UserType’, x = ‘Weekday’, y = ‘Average Duration’) + theme_minimal() + theme(axis.text.x = element_text(angle = 90))

Let’s create a visualization for Number_of_rides Vs Weekday

ggplot(data = cleaned_data, aes(x = weekday, y = Number_of_rides, fill = Average_duration)) + geom_col(position = “dodge”) +
labs(title = ‘Number_of_rides vs Weekday’, x = ‘Weekday’, y = ‘Number_of_rides’) + scale_y_continuous() + theme_minimal() + theme(axis.text.x = element_text(angle = 90))

Ride on Daily Basis

Finding the busiest days of the week

ranked_days <- all_trip %>%group_by(day_of_week,member_casual) %>% summarise(Number_of_rides = n(), Percentage_from_total_rides = percent(n()/nrow(all_trip)), .groups = ‘drop’)%>% arrange(desc(Number_of_rides))

ggplot(data = ranked_days,aes(x = reorder(day_of_week,-Number_of_rides),y = Number_of_rides,fill = day_of_week)) + geom_col(position = ‘dodge’) +scale_y_continuous(labels = scales::comma) + labs(title = ‘Day of the week vs Number of rides’, x = ‘Day of the week’, y = “Number of Rides”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90))

Ride on Monthly basis

all_trip <- all_trip %>% mutate(Month =format(as.Date(started_at), “%B”))

ranked_months <- all_trip %>%group_by(Month) %>% summarise(Number_of_rides = n(), Percentage_from_total_rides = percent(n()/nrow(all_trip)))%>% arrange(-Number_of_rides)

Lets visulaize for Ride on monthly basis

ggplot(data = ranked_months,aes(x = fct_relevel(Month,“January”,“February”,“March”, #Reorder by chronological order “April”,“May”,“June”,“July”,“August”, “September”, “October”,“November”,“December”),y = Number_of_rides,fill = Month)) + geom_col (position = “dodge”) +scale_y_continuous(labels = scales::comma) + labs(title = ‘Months of the year vs Number of rides’, x = ‘Months of the Year’, y = “Number of Rides”) + theme_minimal() + theme(axis.text.x = element_text(angle = 90))

STEP 6

FINDINGS
  • The Avereage duration varies significantly between casual and member riders. Notably, casual riders tend to prolong their bike usage, indicating that members might primarily use the bikes for commuting, whereas casual riders lean towards recreational activities.

  • It is observed that the average duration of bike usage is not solely determined by the number of rides. This is evident in the fact that casual riders spend more time using the bikes compared to member riders, even though they may not have a higher ride count.

  • Examining the ride frequency based on the busiest day of the week highlights a decrease in bike usage during the weekend. This observation further indicates that weekdays see more commuting trips, while recreational use is more prevalent during the weekend.

  • Illustrating the frequency of rides across different months reveals a binomial distribution pattern, particularly concentrated around the summer months. This suggests that there is a notable increase in ride activity during the summer season compared to other months.

STEP 7

RECOMMENDATIONS
  • Promotional efforts for the Annual membership are most effective on weekdays, as the majority of riders tend to take more trips from Monday to Friday.
  • The optimal promotion timeframe is during the summer months, from June to October, as bike rides significantly increase in the sunny weather of summer compared to any other period throughout the year.

ADDITIONAL DATA THAT CAN EXPAND THIS RESEARCH