-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_Analysis.Rmd
352 lines (315 loc) · 13.5 KB
/
Final_Analysis.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
---
title: "Cyclistic bike-share analysis case study"
author: "Tram Anh Nguyen"
date: "2024-03-10"
output:
pdf_document: default
html_document: default
---
# DATA PROCESSING
## Load libraries
```{r}
library(tidyverse)
library(lubridate)
library(ggplot2)
```
## Set my working directory and load 12 .csv files from Febuary 2023 to January 2024
```{r}
setwd('/Users/9360/Desktop/Data Analytics/Google_Data_Analytics/Capstone_Project/Data')
Feb2023_df <- read.csv("202302-divvy-tripdata.csv")
Mar2023_df <- read.csv("202303-divvy-tripdata.csv")
Apr2023_df <- read.csv("202304-divvy-tripdata.csv")
May2023_df <- read.csv("202305-divvy-tripdata.csv")
Jun2023_df <- read.csv("202306-divvy-tripdata.csv")
Jul2023_df <- read.csv("202307-divvy-tripdata.csv")
Aug2023_df <- read.csv("202308-divvy-tripdata.csv")
Sep2023_df <- read.csv("202309-divvy-tripdata.csv")
Oct2023_df <- read.csv("202310-divvy-tripdata.csv")
Nov2023_df <- read.csv("202311-divvy-tripdata.csv")
Dec2023_df <- read.csv("202312-divvy-tripdata.csv")
Jan2024_df <- read.csv("202401-divvy-tripdata.csv")
```
## Merge all uploaded files into a new data frame
```{r}
merged_df <- rbind(Feb2023_df, Mar2023_df, Apr2023_df, May2023_df, Jun2023_df, Jul2023_df, Aug2023_df, Sep2023_df, Oct2023_df, Nov2023_df, Dec2023_df, Jan2024_df)
```
## Remove 12 csv.files to keep the environment organized
```{r}
rm(Feb2023_df, Mar2023_df, Apr2023_df, May2023_df, Jun2023_df, Jul2023_df, Aug2023_df, Sep2023_df, Oct2023_df, Nov2023_df, Dec2023_df, Jan2024_df)
```
## Save the data frame as `merged_data` and get an overview of it
```{r}
merged_data <- merged_df
glimpse(merged_data)
```
There are *13 variables* and *5,674,449 observations* in total.
# DATA CLEANING AND MANIPULATION
## Data Cleaning
### Remove duplicate rows
```{r}
merged_data <- distinct(merged_data)
```
### Remove rows with NA values
```{r}
merged_data <- na.omit(merged_data)
```
### Remove rows with white space or empty string in any columns
```{r}
merged_data <- merged_data[rowSums(sapply(merged_data, function(x) x == "")) == 0, ]
```
### Remove unnecessary columns
```{r}
merged_data <- subset(merged_data, select = -c(ride_id, start_station_id, end_station_id, start_lat, start_lng, end_lat, end_lng))
```
### Save the data frame as `cleaned_data` and get an overview of it
```{r}
cleaned_data <- merged_data
View(cleaned_data)
```
## Data Manipulation
### Create new columns `Ride_length` that calculates the length of each ride by subtracting started_at column from ended_at column and converts it into minutes.
```{r}
# create the column
cleaned_data$ride_length <- difftime(cleaned_data$ended_at, cleaned_data$started_at, units = 'min')
cleaned_data$ride_length <- as.numeric(cleaned_data$ride_length)
# check the first 5 rows
head(cleaned_data, 5)
```
### Create new column `Day_of_week` that extracts the day of the week from started_at column
```{r}
# create the column
cleaned_data$day_of_week <- wday(cleaned_data$started_at, label = TRUE, abbr = FALSE)
# check the first 5 rows
head(cleaned_data, 5)
```
### Create new column `Month` that extracts the month from started_at column
```{r}
# create the column
cleaned_data$month <- format(as.Date(cleaned_data$started_at), "%m")
# check the first 5 rows
head(cleaned_data, 5)
```
### Create new column `Day` that extracts the day from started_at column.
```{r}
# create the column
cleaned_data$day <- format(as.Date(cleaned_data$started_at), "%d")
# check the first 5 rows
head(cleaned_data, 5)
```
### Create new column `Hour` that extracts the hour from started_at column
```{r}
# create the column
cleaned_data$hour <- format(as.POSIXct(cleaned_data$started_at), format = "%H")
# check the first 5 rows
head(cleaned_data, 5)
```
### Round_trip: identify whether the route is one-way or round-trip
```{r}
# create the column
cleaned_data$round_trip <- ifelse(cleaned_data$start_station_name == cleaned_data$end_station_name, 'Yes', 'No')
# check the first 5 rows
head(cleaned_data, 5)
```
## View the data frame
```{r}
View(cleaned_data)
```
## Remove any rows which ride_length is negative
```{r}
cleaned_data <- cleaned_data %>%
filter(ride_length >= 0)
```
## Save the new data frame as `final_data`
```{r}
final_data <- cleaned_data
```
# DATA ANALYZATION
## Total Rides & Average Ride Length
```{r}
final_data %>%
summarize(total_rides = nrow(final_data),average_ride_length = round(mean(ride_length), digits = 2))
```
## Total Rides and Average Ride Length by User Type
```{r}
final_data %>%
group_by(member_casual) %>%
summarise(total_rides = length(member_casual),
percentage = round((length(member_casual) / nrow(final_data)) * 100, digits = 2),
average_ride_length = round(mean(ride_length), digits = 2))
```
## Total Rides and Average Ride Length by Rideable Type and User Type.
```{r}
final_data %>%
group_by(member_casual, rideable_type) %>%
summarise(total_rides = length(member_casual),
percentage = round((length(rideable_type) / nrow(final_data)) * 100, digits = 2),
average_ride_length = round(mean(ride_length), digits = 2))
```
# DATA VISUALIZATION
## Total Rides by Day of the Week and User Type
```{r}
final_data %>%
group_by(member_casual, day_of_week) %>%
summarize(number_of_ride = n()) %>%
ggplot(aes(factor(day_of_week,levels = c("Sunday", "Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday")), number_of_ride, fill = member_casual)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.7) +
facet_wrap(~ member_casual) +
geom_text(aes(label = format(number_of_ride, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 2.0) +
labs(x = "Day of week", y = "Number of Rides",
title = "Cyclistic - Total Rides (Feb 2023 - Jan 2024)",
subtitle = "By Day of Week and User Type") +
scale_fill_manual(values = c("#de6e56","#0b3c5d")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0, 550000)) +
theme_classic(base_size = 10) +
coord_flip()
ggsave("Total_Rides_by_Day_of_the_Week_and_User_Type.png", plot = last_plot(), dpi = 300)
```
## Average Ride Length by Day of the Week and User Type
```{r}
final_data %>%
group_by(member_casual, day_of_week) %>%
summarize(average_ride_length = round(mean(ride_length), digits = 2))%>%
ggplot(aes(factor(day_of_week,levels = c("Sunday","Saturday", "Friday","Thursday","Wednesday","Tuesday","Monday")),average_ride_length, fill = member_casual)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.7) +
facet_wrap(~ member_casual) +
geom_text(aes(label = format(average_ride_length, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 2.0) +
labs(x = "Day of week", y = "Average Ride Length (min)",
title = "Cyclistic - Average Ride Length (Feb 2023 - Jan 2024)",
subtitle = "By Day of Week and User Type") +
scale_fill_manual(values = c("#de6e56","#0b3c5d")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0,30)) +
theme_classic(base_size = 10) +
coord_flip()
ggsave("Average_Ride_Length_by_Day_of_the_Week_and_User_Type.png", plot = last_plot(), dpi = 300)
```
## Total Rides by Month and User Type
```{r}
final_data %>%
group_by(member_casual, month) %>%
summarize(number_of_ride = n()) %>%
ggplot(aes(month, number_of_ride, fill = member_casual)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.7) +
facet_wrap(~ member_casual) +
geom_text(aes(label = format(number_of_ride, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 2.0) +
labs(x = "Month", y = "Number of Rides",
title = "Cyclistic - Total Rides (Feb 2023 - Jan 2024)",
subtitle = "By Month and User Type") +
scale_fill_manual(values = c("#de6e56","#0b3c5d")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0, 550000)) +
theme_classic(base_size = 10) +
coord_flip()
ggsave("Total_Rides_by_Month_and_User_Type.png", plot = last_plot(), dpi = 300)
```
## Average Ride Length by Month and User Type
```{r}
final_data %>%
group_by(member_casual, month) %>%
summarize(average_ride_length = round(mean(ride_length), digits = 2))%>%
ggplot(aes(month,average_ride_length, fill = member_casual)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.7) +
facet_wrap(~ member_casual) +
geom_text(aes(label = format(average_ride_length, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 2.0) +
labs(x = "Month", y = "Average Ride Length (min)",
title = "Cyclistic - Average Ride Length (Feb 2023 - Jan 2024)",
subtitle = "By Month and User Type") +
scale_fill_manual(values = c("#de6e56","#0b3c5d")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0,30)) +
theme_classic(base_size = 10) +
coord_flip()
ggsave("Average_Ride_Length_by_Month_and_User_Type.png", plot = last_plot(), dpi = 300)
```
## Total Rides by User Type per Hour
```{r}
final_data %>%
group_by(member_casual, hour) %>%
summarize(number_of_ride = n()) %>%
ggplot(aes(hour, number_of_ride, fill = member_casual)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.7) +
facet_wrap(~ member_casual) +
geom_text(aes(label = format(number_of_ride, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 2.0) +
labs(x = "Hour", y = "Number of Rides",
title = "Cyclistic - Total Rides (Feb 2023 - Jan 2024)",
subtitle = "By Hour and User Type") +
scale_fill_manual(values = c("#de6e56","#0b3c5d")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0, 350000)) +
theme_classic(base_size = 10) +
coord_flip()
ggsave("Total_Rides_by_Hour_and_User_Type.png", plot = last_plot(), dpi = 300)
```
## Average Ride Length by User Type per Hour
```{r}
final_data %>%
group_by(member_casual, hour) %>%
summarize(average_ride_length = round(mean(ride_length), digits = 2))%>%
ggplot(aes(hour,average_ride_length, fill = member_casual)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.7) +
facet_wrap(~ member_casual) +
geom_text(aes(label = format(average_ride_length, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 2.0) +
labs(x = "Hour", y = "Average Ride Length (min)",
title = "Cyclistic - Average Ride Length (Feb 2023 - Jan 2024)",
subtitle = "By Hour and User Type") +
scale_fill_manual(values = c("#de6e56","#0b3c5d")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0,30)) +
theme_classic(base_size = 10) +
coord_flip()
ggsave("Average_Ride_Length_by_Hour_and_User_Type.png", plot = last_plot(), dpi = 300)
```
## Total Rides & Average Ride Length of Round-trip and One-way Route by User Type
```{r}
final_data %>%
group_by(member_casual, round_trip) %>%
summarise(total_rides = length(round_trip),
average_ride_length = round(mean(ride_length), digits = 2))
```
## Top 10 Most Frequent Routes for Member User
```{r}
final_data <- final_data %>%
mutate(route = paste(start_station_name, "->", end_station_name))
# group by the new route column, filter for list of top 10 routes
top_routes_member <- final_data %>%
filter(member_casual == "member") %>%
group_by(route, member_casual, round_trip) %>%
summarize(number_of_ride = n()) %>%
ungroup() %>%
arrange(desc(number_of_ride)) %>%
slice_max(order_by = number_of_ride, n = 10)
# plot the top 10 routes
ggplot(top_routes_member, aes(x = reorder(route, number_of_ride), y = number_of_ride, fill = round_trip)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.5) +
geom_text(aes(label = format(number_of_ride, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 1.7) +
labs(x = "Route", y = "Number of Rides",
title = "Cyclistic - Top 10 Most Frequent Routes",
subtitle = "For Member Users") +
scale_fill_manual(values = c("#c1c1f3","#ffd1dc")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0,10000)) +
theme_classic(base_size = 10) +
theme(axis.text.y = element_text(size = 6)) +
coord_flip()
ggsave("Top_10_Most_Frequent_Routes_For_Member_Users.png", plot = last_plot(), dpi = 300)
```
## Top 10 Most Frequent Routes for Casual User
```{r}
final_data <- final_data %>%
mutate(route = paste(start_station_name, "->", end_station_name))
# group by the new route column, filter for list of top 10 routes
top_routes_member <- final_data %>%
filter(member_casual == "casual") %>%
group_by(route, member_casual, round_trip) %>%
summarize(number_of_ride = n()) %>%
ungroup() %>%
arrange(desc(number_of_ride)) %>%
slice_max(order_by = number_of_ride, n = 10)
# plot the top 10 routes
ggplot(top_routes_member, aes(x = reorder(route, number_of_ride), y = number_of_ride, fill = round_trip)) +
geom_bar(stat = "identity", position = position_dodge(preserve = 'single'), width = 0.5) +
geom_text(aes(label = format(number_of_ride, big.mark = ",")), position = position_dodge(width = 0.7), hjust = -0.1, size = 1.7) +
labs(x = "Route", y = "Number of Rides",
title = "Cyclistic - Top 10 Most Frequent Routes",
subtitle = "For Casual Users") +
scale_fill_manual(values = c("#c1c1f3","#ffd1dc")) +
scale_y_continuous(labels = scales::comma_format(), limits = c(0,10000)) +
theme_classic(base_size = 10) +
theme(axis.text.y = element_text(size = 6)) +
coord_flip()
ggsave("Top_10_Most_Frequent_Routes_For_Casual_Users.png", plot = last_plot(), dpi = 300)
```