-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis of Bike Sharing Company - Cyclistic.Rmd
248 lines (184 loc) · 8.15 KB
/
Analysis of Bike Sharing Company - Cyclistic.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
---
title: 'Analysis of Bike Sharing Company: Cyclistic'
author: "Wale Adio"
date: "2023-08-19"
output:
html_document:
toc: yes
number_sections: yes
---
```{r setup, include=FALSE}
library(tidyverse)
library(lubridate)
library(ggplot2)
library(dplyr)
knitr::opts_chunk$set(echo = TRUE)
# Set the root directory for notebook chunks
knitr::opts_knit$set(root.dir = "/Users/Wale/Downloads/Projects/Google Analytics/Cyclistic/Divvy_Project")
```
# Data Analysis
## Installing and Loading Packages
Analysis commenced by gathering and cleaning data sets from multiple quarters **(2019 q2, q3, q4 & 2020 q1)** to ensure uniform data. The `Tidyverse` package was utilized for data importation and wrangling. The `Lubridate` package was utilized for data handling attributes, while `Dplyr` and `Ggplot2` packages were utilized for data manipulation and visualization respectively.
## Setting Directory
First the directory needs to be established for data importation
```{r}
setwd("/Users/Wale/Downloads/Projects/Google Analytics/Cyclistic/Divvy_Project")
```
## Importing Datasets
The data set were imported, with the data set capturing bike sharing data from 2019 q2 to 2020 q1.
```{r echo=TRUE, message=FALSE, warning=FALSE}
# Uploading csv files
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")
# Viewing column names and structure
str(q2_2019)
colnames(q2_2019)
str(q3_2019)
colnames(q3_2019)
str(q4_2019)
colnames(q4_2019)
str(q1_2020)
colnames(q1_2020)
```
## Data Cleaning and Wrangling
This section involves renaming columns to ensure uniformity, ensuring data types are consistent, merging data sets and removing irrelevant columns and bad data. Additionally, for data aggregation, new columns would be created.
### Renaming Columns
```{r echo=TRUE, message=FALSE, warning=FALSE}
#Columns in q2_2019, q3_2019 & q4_2019 to be renamed to make them consistent with q1_2020
q2_2019 <- rename(q2_2019,
ride_id = "X01...Rental.Details.Rental.ID",
rideable_type = "X01...Rental.Details.Bike.ID",
started_at = "X01...Rental.Details.Local.Start.Time",
ended_at = "X01...Rental.Details.Local.End.Time",
start_station_name = "X03...Rental.Start.Station.Name",
start_station_id = "X03...Rental.Start.Station.ID",
end_station_name = "X02...Rental.End.Station.Name",
end_station_id = "X02...Rental.End.Station.ID",
member_casual = "User.Type")
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")
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")
```
### Converting Data Types
```{r echo=TRUE, message=FALSE, warning=FALSE}
##Converting data types in q2_2019, q3_2019 & q4_2019 to match data types in q1_2020
q2_2019 <- mutate(q2_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))
q4_2019 <- mutate(q4_2019, ride_id = as.character(ride_id),
rideable_type = as.character(rideable_type))
```
### Combining Respective Data Sets
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips <- bind_rows(q1_2020, q2_2019, q3_2019, q4_2019)
```
### Removing Irrelevant Columns
```{r echo=TRUE, message=FALSE, warning=FALSE}
#birthyear, gender, start_lat, start_lng, end_lat, end_lng, member.gender, X05...member.details.member.birthday.year, tripduration, X01...Rental.details.duration.in.seconds.uncapped
bike_trips <- bike_trips %>%
select(-c(birthyear, gender, start_lat, start_lng, end_lat, end_lng, Member.Gender,
"X05...Member.Details.Member.Birthday.Year", "tripduration", "X01...Rental.Details.Duration.In.Seconds.Uncapped"))
```
### Inspecting Combined Data Set
```{r echo=TRUE, message=FALSE, warning=FALSE}
summary(bike_trips)
str(bike_trips)
head(bike_trips)
```
### Ensuring consistency in member_casual column
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips <- bike_trips %>%
mutate(member_casual = recode(member_casual,
"Subscriber" = "member",
"Customer" = "casual"))
```
### Creating New Columns
New columns **(Day, Month & Year)** were created to enable data aggregation
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips$date <- as.Date(bike_trips$started_at)
bike_trips$month <- format(as.Date(bike_trips$date),"%m")
bike_trips$day <- format(as.Date(bike_trips$date),"%d")
bike_trips$year <- format(as.Date(bike_trips$date),"%Y")
bike_trips$day_of_week <- format(as.Date(bike_trips$date),"%A")
#Creating new column to calculate duration of each ride
bike_trips$ride_length <- difftime(bike_trips$ended_at,bike_trips$started_at)
is.factor(bike_trips$ride_length)
bike_trips$ride_length <- as.numeric(as.character(bike_trips$ride_length))
is.numeric(bike_trips$ride_length)
```
### Removing Bad Data
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips_v2 <- bike_trips[!(bike_trips$start_station_name == "HQ QR" | bike_trips$ride_length<0),]
```
### Arranging Days of the Week in Order
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips_v2$day_of_week <- ordered(bike_trips_v2$day_of_week, levels=c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))
```
# Descriptive Analysis
```{r echo=TRUE, message=FALSE, warning=FALSE}
mean(bike_trips_v2$ride_length)
median(bike_trips_v2$ride_length)
max(bike_trips_v2$ride_length)
min(bike_trips_v2$ride_length)
summary(bike_trips_v2$ride_length)
```
### Comparing Members vs Casual Riders
```{r message=FALSE, warning=FALSE}
aggregate(bike_trips_v2$ride_length ~ bike_trips_v2$member_casual, FUN = mean)
aggregate(bike_trips_v2$ride_length ~ bike_trips_v2$member_casual, FUN = median)
aggregate(bike_trips_v2$ride_length ~ bike_trips_v2$member_casual, FUN = max)
aggregate(bike_trips_v2$ride_length ~ bike_trips_v2$member_casual, FUN = min)
```
### Comparing Average Ride Times by Day for Members vs Casual Rider
```{r echo=TRUE, message=FALSE, warning=FALSE}
aggregate(bike_trips_v2$ride_length ~ bike_trips_v2$member_casual + bike_trips_v2$day_of_week, FUN = mean)
```
### Analyzing Ridership Data by Type & Weekday
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips_v2 %>%
mutate(weekday = wday(started_at, label = TRUE)) %>%
group_by(member_casual, weekday) %>%
summarise(number_of_rides = n(), average_duration = mean(ride_length)) %>%
arrange(member_casual, weekday)
```
# Visualizing Findings
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips_v2 %>%
mutate(weekday = wday(started_at, label = TRUE)) %>%
group_by(member_casual, weekday) %>%
summarise(number_of_rides = n(),
average_duration = mean(ride_length)) %>%
arrange(member_casual, weekday) %>%
ggplot(aes(x = weekday, y = average_duration, fill = member_casual)) +
geom_col(position = "dodge")
```
### Visualizing by Rider Type
```{r echo=TRUE, message=FALSE, warning=FALSE}
bike_trips_v2 %>%
mutate(weekday = wday(started_at, label = TRUE)) %>%
group_by(member_casual, weekday) %>%
summarise(number_of_rides = n(),
average_duration = mean(ride_length)) %>%
ggplot(aes(x = weekday, y = number_of_rides, fill = member_casual)) +
geom_col(position = "dodge")
```