This R code is part of the Cyclistic Bike Share Analysis Case Study which was completed as a requirement for the Google Data Analytics Professional Certificate.
For this case study, 12 monthly files (October 2022 to Sept 2023) were downloaded from https://divvy-tripdata.s3.amazonaws.com/index.html
Two R packages were used:
library(tidyverse)
## -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
## v dplyr 1.1.2 v readr 2.1.4
## v forcats 1.0.0 v stringr 1.5.1
## v ggplot2 3.4.4 v tibble 3.2.1
## v lubridate 1.9.2 v tidyr 1.3.0
## v purrr 1.0.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
Uploading the Divvy datasets (csv files)
Oct_2022 = read_csv("202210-divvy-tripdata.csv")
## Rows: 558685 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Nov_2022 = read_csv("202211-divvy-tripdata.csv")
## Rows: 337735 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Dec_2022 = read_csv("202212-divvy-tripdata.csv")
## Rows: 181806 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Jan_2023 = read_csv("202301-divvy-tripdata.csv")
## Rows: 190301 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Feb_2023 = read_csv("202302-divvy-tripdata.csv")
## Rows: 190445 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Mar_2023 = read_csv("202303-divvy-tripdata.csv")
## Rows: 258678 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Apr_2023 = read_csv("202304-divvy-tripdata.csv")
## Rows: 426590 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
May_2023 = read_csv("202305-divvy-tripdata.csv")
## Rows: 604827 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Jun_2023 = read_csv("202306-divvy-tripdata.csv")
## Rows: 719618 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Jul_2023 = read_csv("202307-divvy-tripdata.csv")
## Rows: 767650 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Aug_2023 = read_csv("202308-divvy-tripdata.csv")
## Rows: 771693 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Sep_2023 = read_csv("202309-divvy-tripdata.csv")
## Rows: 666371 Columns: 13
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (7): ride_id, rideable_type, start_station_name, start_station_id, end_...
## dbl (4): start_lat, start_lng, end_lat, end_lng
## dttm (2): started_at, ended_at
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Comparing the column names for each of the datasets to ensure that there are no inconsistencies
colnames(Oct_2022)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Dec_2022)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Jan_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Feb_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Mar_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Apr_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(May_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Jun_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Jul_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Aug_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
colnames(Sep_2023)
## [1] "ride_id" "rideable_type" "started_at"
## [4] "ended_at" "start_station_name" "start_station_id"
## [7] "end_station_name" "end_station_id" "start_lat"
## [10] "start_lng" "end_lat" "end_lng"
## [13] "member_casual"
Combining all the separate files into one single data frame and then creating a copy for further processing
data_12m_raw = rbind(Oct_2022,Nov_2022,Dec_2022,Jan_2023,Feb_2023,Mar_2023,Apr_2023,May_2023,Jun_2023,Jul_2023,Aug_2023,Sep_2023)
data_12m = data_12m_raw
Dropping the standalone flies from the environment
remove(Oct_2022,Nov_2022,Dec_2022,Jan_2023,Feb_2023,Mar_2023,Apr_2023,May_2023,Jun_2023,Jul_2023,Aug_2023,Sep_2023)
Renaming Certain columns
data_12m = data_12m %>%
rename(trip_id = ride_id)%>%
rename(bike_type = rideable_type)%>%
rename(start_time = started_at)%>%
rename(end_time = ended_at)%>%
rename(user_type = member_casual)
Creating Additional Columns for analysis
data_12m$ride_length = difftime(data_12m$end_time,data_12m$start_time, units = "mins")
data_12m$start_hour = hour(data_12m$start_time)
data_12m = data_12m %>%
mutate(daytime =
case_when(start_hour < 5 ~ "Night",
start_hour < 12 ~ "Morning",
start_hour < 17 ~ "Afternoon",
start_hour < 21 ~ "Evening",
start_hour < 24 ~ "Night")
)
data_12m$weekday = wday(data_12m$start_time,label = TRUE,abbr = TRUE,week_start = 1)
data_12m$month = month(data_12m$start_time, label = TRUE, abbr = TRUE)
data_12m = data_12m %>%
mutate(season =
case_when(month == "Jan" ~ "Winter",
month == "Feb" ~ "Winter",
month == "Mar" ~ "Spring",
month == "Apr" ~ "Spring",
month == "May" ~ "Spring",
month == "Jun" ~ "Summer",
month == "Jul" ~ "Summer",
month == "Aug" ~ "Summer",
month == "Sep" ~ "Fall",
month == "Oct" ~ "Fall",
month == "Nov" ~ "Fall",
month == "Dec" ~ "Winter")
)
data_12m$quarter = quarter(data_12m$start_time, type = "year.quarter", fiscal_start = 1)
#note: did not end up using for analysis as results are almost similar to season
Removing Duplicates, NA and entries with Ride Length less than or equal to 0
data_12m = distinct(data_12m)
data_12m = na.omit(data_12m)
data_12m = data_12m[data_12m$ride_length > 0,]
Checking Data Types and fixing wrong data types
str(data_12m)
## tibble [4,290,979 x 20] (S3: tbl_df/tbl/data.frame)
## $ trip_id : chr [1:4290979] "A50255C1E17942AB" "DB692A70BD2DD4E3" "3C02727AAF60F873" "47E653FDC2D99236" ...
## $ bike_type : chr [1:4290979] "classic_bike" "electric_bike" "electric_bike" "electric_bike" ...
## $ start_time : POSIXct[1:4290979], format: "2022-10-14 17:13:30" "2022-10-01 16:29:26" ...
## $ end_time : POSIXct[1:4290979], format: "2022-10-14 17:19:39" "2022-10-01 16:49:06" ...
## $ start_station_name: chr [1:4290979] "Noble St & Milwaukee Ave" "Damen Ave & Charleston St" "Hoyne Ave & Balmoral Ave" "Rush St & Cedar St" ...
## $ start_station_id : chr [1:4290979] "13290" "13288" "655" "KA1504000133" ...
## $ end_station_name : chr [1:4290979] "Larrabee St & Division St" "Damen Ave & Cullerton St" "Western Ave & Leland Ave" "Orleans St & Chestnut St (NEXT Apts)" ...
## $ end_station_id : chr [1:4290979] "KA1504000079" "13089" "TA1307000140" "620" ...
## $ start_lat : num [1:4290979] 41.9 41.9 42 41.9 41.9 ...
## $ start_lng : num [1:4290979] -87.7 -87.7 -87.7 -87.6 -87.6 ...
## $ end_lat : num [1:4290979] 41.9 41.9 42 41.9 41.9 ...
## $ end_lng : num [1:4290979] -87.6 -87.7 -87.7 -87.6 -87.6 ...
## $ user_type : chr [1:4290979] "member" "casual" "member" "member" ...
## $ ride_length : 'difftime' num [1:4290979] 6.15 19.6666666666667 7.83333333333333 6.21666666666667 ...
## ..- attr(*, "units")= chr "mins"
## $ start_hour : int [1:4290979] 17 16 18 7 18 15 15 17 9 12 ...
## $ daytime : chr [1:4290979] "Evening" "Afternoon" "Evening" "Morning" ...
## $ weekday : Ord.factor w/ 7 levels "Mon"<"Tue"<"Wed"<..: 5 6 3 1 4 4 4 3 6 1 ...
## $ month : Ord.factor w/ 12 levels "Jan"<"Feb"<"Mar"<..: 10 10 10 10 10 10 10 10 10 10 ...
## $ season : chr [1:4290979] "Fall" "Fall" "Fall" "Fall" ...
## $ quarter : num [1:4290979] 2022 2022 2022 2022 2022 ...
## - attr(*, "na.action")= 'omit' Named int [1:1382948] 2848 2849 2850 2851 2852 2854 2856 4359 4360 4361 ...
## ..- attr(*, "names")= chr [1:1382948] "2848" "2849" "2850" "2851" ...
Converting Ride Length to numeric for further analysis as it was the incorrect data type
data_12m$ride_length = as.numeric(data_12m$ride_length)
Some basic analysis of the dataset
#total rides
nrow(data_12m)
## [1] 4290979
#analysis on ride_length (in minutes)
summary(data_12m$ride_length)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.017 5.633 9.833 15.968 17.550 12136.300
#rides by user_type
data_12m %>%
group_by (user_type) %>%
summarise(rides=n())
## # A tibble: 2 x 2
## user_type rides
## <chr> <int>
## 1 casual 1548865
## 2 member 2742114
#rides by bike_type
data_12m %>%
group_by (bike_type) %>%
summarise(rides=n())
## # A tibble: 3 x 2
## bike_type rides
## <chr> <int>
## 1 classic_bike 2573959
## 2 docked_bike 96186
## 3 electric_bike 1620834
#avg_ride_length by user_type
data_12m %>%
group_by (user_type) %>%
summarise(avg_ride_length= mean(ride_length))
## # A tibble: 2 x 2
## user_type avg_ride_length
## <chr> <dbl>
## 1 casual 22.8
## 2 member 12.1
#rides by bike_type and user_type
data_12m %>%
group_by (bike_type) %>%
count(user_type)
## # A tibble: 5 x 3
## # Groups: bike_type [3]
## bike_type user_type n
## <chr> <chr> <int>
## 1 classic_bike casual 834698
## 2 classic_bike member 1739261
## 3 docked_bike casual 96186
## 4 electric_bike casual 617981
## 5 electric_bike member 1002853
#rides and avg_ride_length by hour
data_12m %>%
group_by(user_type, start_hour) %>%
summarise(rides=n(), avg_ride_length=mean(ride_length))%>%
arrange (start_hour)
## `summarise()` has grouped output by 'user_type'. You can override using the
## `.groups` argument.
## # A tibble: 48 x 4
## # Groups: user_type [2]
## user_type start_hour rides avg_ride_length
## <chr> <int> <int> <dbl>
## 1 casual 0 25891 20.6
## 2 member 0 23671 11.5
## 3 casual 1 16711 20.5
## 4 member 1 14186 11.8
## 5 casual 2 9571 20.8
## 6 member 2 7752 12.1
## 7 casual 3 4909 19.6
## 8 member 3 4828 12.6
## 9 casual 4 3675 17.3
## 10 member 4 5962 11.8
## # i 38 more rows
#rides and avg_ride_length by daytime
data_12m %>%
group_by(user_type, daytime) %>%
summarise(rides=n(), avg_ride_length=mean(ride_length))%>%
arrange (daytime)
## `summarise()` has grouped output by 'user_type'. You can override using the
## `.groups` argument.
## # A tibble: 8 x 4
## # Groups: user_type [2]
## user_type daytime rides avg_ride_length
## <chr> <chr> <int> <dbl>
## 1 casual Afternoon 578309 25.2
## 2 member Afternoon 882416 12.4
## 3 casual Evening 441844 21.3
## 4 member Evening 801918 12.8
## 5 casual Morning 327329 22.2
## 6 member Morning 815428 11.2
## 7 casual Night 201383 20.1
## 8 member Night 242352 12.0
#rides and avg_ride_length by weekday
data_12m %>%
group_by(user_type, weekday) %>%
summarise(rides=n(), avg_ride_length=mean(ride_length))%>%
arrange (weekday)
## `summarise()` has grouped output by 'user_type'. You can override using the
## `.groups` argument.
## # A tibble: 14 x 4
## # Groups: user_type [2]
## user_type weekday rides avg_ride_length
## <chr> <ord> <int> <dbl>
## 1 casual Mon 176484 22.4
## 2 member Mon 379441 11.5
## 3 casual Tue 176835 20.2
## 4 member Tue 431613 11.6
## 5 casual Wed 182760 19.3
## 6 member Wed 440887 11.6
## 7 casual Thu 201629 20.0
## 8 member Thu 441666 11.6
## 9 casual Fri 230947 22.2
## 10 member Fri 391882 12.1
## 11 casual Sat 323663 25.8
## 12 member Sat 354252 13.6
## 13 casual Sun 256547 26.2
## 14 member Sun 302373 13.6
#rides and avg_ride_length by month
data_12m %>%
group_by(user_type, month) %>%
summarise(rides=n(), avg_ride_length=mean(ride_length))%>%
arrange (month)
## `summarise()` has grouped output by 'user_type'. You can override using the
## `.groups` argument.
## # A tibble: 24 x 4
## # Groups: user_type [2]
## user_type month rides avg_ride_length
## <chr> <ord> <int> <dbl>
## 1 casual Jan 29618 14.9
## 2 member Jan 118662 10.0
## 3 casual Feb 32774 17.7
## 4 member Feb 116778 10.4
## 5 casual Mar 46786 16.7
## 6 member Mar 153647 10.2
## 7 casual Apr 110526 22.6
## 8 member Apr 213647 11.6
## 9 casual May 177025 24.5
## 10 member May 286162 12.7
## # i 14 more rows
#rides and avg_ride_length by season
data_12m %>%
group_by(user_type, season) %>%
summarise(rides=n(), avg_ride_length=mean(ride_length))%>%
arrange (season)
## `summarise()` has grouped output by 'user_type'. You can override using the
## `.groups` argument.
## # A tibble: 8 x 4
## # Groups: user_type [2]
## user_type season rides avg_ride_length
## <chr> <chr> <int> <dbl>
## 1 casual Fall 421783 21.3
## 2 member Fall 754762 11.9
## 3 casual Spring 334337 22.8
## 4 member Spring 653456 11.7
## 5 casual Summer 698851 24.6
## 6 member Summer 994565 13.2
## 7 casual Winter 93894 15.8
## 8 member Winter 339331 10.2
Creating a new dataset to analyse ride routes by combining start_station and end_station
#create ride route column
ride_route = paste(data_12m$start_station_name, data_12m$end_station_name, sep = " to ")
#create new dataframe with ride_route and data_12m
ride_route_df = cbind(data_12m,ride_route)
#top 7 start_stations
ride_route_df %>%
group_by(start_station_name,start_station_id, user_type) %>%
summarise(rides= n()) %>%
filter(user_type == "casual") %>%
arrange(desc(rides)) %>%
head(n=7)%>%
select(start_station_name, start_station_id, rides)
## `summarise()` has grouped output by 'start_station_name', 'start_station_id'.
## You can override using the `.groups` argument.
## # A tibble: 7 x 3
## # Groups: start_station_name, start_station_id [7]
## start_station_name start_station_id rides
## <chr> <chr> <int>
## 1 Streeter Dr & Grand Ave 13022 44774
## 2 DuSable Lake Shore Dr & Monroe St 13300 29129
## 3 Michigan Ave & Oak St 13042 21390
## 4 Millennium Park 13008 19445
## 5 DuSable Lake Shore Dr & North Blvd LF-005 18941
## 6 Shedd Aquarium 15544 17182
## 7 Theater on the Lake TA1308000001 15250
#top 7 end_stations
ride_route_df %>%
group_by(end_station_name,end_station_id, user_type) %>%
summarise(rides= n()) %>%
filter(user_type == "casual") %>%
arrange(desc(rides)) %>%
head(n=7)%>%
select(end_station_name, end_station_id, rides)
## `summarise()` has grouped output by 'end_station_name', 'end_station_id'. You
## can override using the `.groups` argument.
## # A tibble: 7 x 3
## # Groups: end_station_name, end_station_id [7]
## end_station_name end_station_id rides
## <chr> <chr> <int>
## 1 Streeter Dr & Grand Ave 13022 49130
## 2 DuSable Lake Shore Dr & Monroe St 13300 26741
## 3 Michigan Ave & Oak St 13042 22903
## 4 DuSable Lake Shore Dr & North Blvd LF-005 22463
## 5 Millennium Park 13008 21812
## 6 Theater on the Lake TA1308000001 17007
## 7 Shedd Aquarium 15544 15435
#top 7 routes
ride_route_df %>%
group_by(ride_route, user_type) %>%
summarise(rides= n()) %>%
filter(user_type == "casual") %>%
arrange(desc(rides)) %>%
head(n=7)%>%
select(ride_route, rides)
## `summarise()` has grouped output by 'ride_route'. You can override using the
## `.groups` argument.
## # A tibble: 7 x 2
## # Groups: ride_route [7]
## ride_route rides
## <chr> <int>
## 1 Streeter Dr & Grand Ave to Streeter Dr & Grand Ave 8941
## 2 DuSable Lake Shore Dr & Monroe St to DuSable Lake Shore Dr & Monroe St 6860
## 3 DuSable Lake Shore Dr & Monroe St to Streeter Dr & Grand Ave 4777
## 4 Michigan Ave & Oak St to Michigan Ave & Oak St 4309
## 5 Millennium Park to Millennium Park 3534
## 6 Dusable Harbor to Dusable Harbor 2701
## 7 Montrose Harbor to Montrose Harbor 2497
Use the code: write.csv(data_12m,‘CyclisticBikeShareData_12m.csv’)
Note: Exporting data_12m and not ride_route_df as route data is not needed for the visualization