-
Notifications
You must be signed in to change notification settings - Fork 0
/
code
291 lines (236 loc) · 6.94 KB
/
code
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
library("lubridate")
library("janitor")
library("tidyverse")
library("dplyr")
library("readr")
library("tidyr")
library("ggplot2")
library("stringr")
library("skimr")
library("scales")
library("data.table")
library("plyr")
library("viridis")
setwd("C:/Users/admin/Documents/bikeshare")
getwd()
apr <- read.csv("202004-divvy-tripdata.csv")
may <- read.csv("202005-divvy-tripdata.csv")
jun <- read.csv("202006-divvy-tripdata.csv")
jul <- read.csv("202007-divvy-tripdata.csv")
aug <- read.csv("202008-divvy-tripdata.csv")
sept <- read.csv("202009-divvy-tripdata.csv")
oct <- read.csv("202010-divvy-tripdata.csv")
nov <- read.csv("202011-divvy-tripdata.csv")
dec <- read.csv("202012-divvy-tripdata.csv")
jan <- read.csv("202101-divvy-tripdata.csv")
feb <- read.csv("202102-divvy-tripdata.csv")
mar <- read.csv("202103-divvy-tripdata.csv")
trips <- rbind(apr,may,jun,jul,aug,sept,oct,nov,dec,jan,feb,mar)
trips <- remove_empty(trips,which = c("cols"))
trips <- remove_empty(trips,which = c("rows"))
colnames(trips)[13] <- "user"
colnames(trips)[2] <- "bike"
colnames(trips)[3] <- "start_time"
colnames(trips)[4] <- "end_time"
colnames(trips)[5] <- "start_station"
colnames(trips)[7] <- "end_station"
trips = trips %>%
mutate(
start_time = ymd_hms(as_datetime(start_time)),
end_time = ymd_hms(as_datetime(end_time))
)
trips = trips %>%
mutate(
bike = as_factor(bike),
user = as_factor(user)
)
trips_1 = trips %>%
mutate(
hours = hour(start_time),
weekday = lubridate::wday(start_time, label = T, abbr = F),
month = lubridate::month(start_time, label = T, abbr =F)
)
trips_1 = trips_1 %>%
mutate(
trip_time = difftime(end_time, start_time, units = "mins")
)
glimpse(trips_1)
colnames(trips_1)
head(trips_1)
colSums(is.na(trips_1))
remove(trips)
trips_1 <- distinct(trips_1)
trips_2 = trips_1 %>%
filter(between(trip_time, 1, 1440))
# ANALYZE
glimpse(trips_2)
max(trips_2$trip_time)
min(trips_2$trip_time)
colSums(is.na(trips_2))
skim_without_charts(trips_2)
mean(trips_2$trip_time)
median(trips_2$trip_time)
aggregate(trips_2$trip_time ~ trips_2$user, FUN = mean)
analyse_table <- trips_2 %>%
select (ride_id,bike,start_station,end_station,user,trip_time,hours,weekday,month)
glimpse(analyse_table)
colnames(analyse_table)
remove(trips_1)
remove(trips_2)
# VISUALIZE
ride_hours = analyse_table %>%
group_by(user, hours) %>%
dplyr::summarise(no_of_rides = n(),
avg_trip = mean(trip_time),
total_trip = sum(trip_time)
)
ride_hours %>%
ggplot(aes(hours, no_of_rides, fill = user))+
geom_col(position = "dodge")+
scale_y_continuous()+
labs(
title = "Number of Trips per Hour",
subtitle = "Number of trips for every hour and by user",
caption = "Figure 1",
x = "hour of the day",
y = "number of rides",
)
ride_hours %>%
ggplot(aes(hours, avg_trip, fill = user))+
geom_col(position = "dodge")+
scale_y_continuous()+
labs(
title = "Average trips per Hour",
subtitle = "Average trip time for every hour and by user",
caption = "Figure 2",
x = "hour of the day",
y = "avg time of rides",
)
ride_hours %>%
ggplot(aes(hours, total_trip, fill = user))+
geom_col(position = "dodge")+
scale_y_continuous()+
labs(
title = "Total Trip Time per Hour",
subtitle = "Sum of trip time for every hour and by user",
caption = "Figure 3",
x = "hour of the day",
y = "total time of rides",
)
ride_week = analyse_table %>%
group_by(user, weekday) %>%
dplyr::summarise(no_of_rides = n(),
avg_trip = mean(trip_time),
total_trip = sum(trip_time)
)
ride_week %>%
ggplot(aes(weekday, no_of_rides, fill=user)) +
geom_col(position="dodge") +
scale_y_continuous(labels=comma) +
labs(
title = "No. of Rides by Days of a Week",
subtitle = "Number of rides on each weekday by users",
caption = "Figure 4",
x = "Days of a Week",
y = "Number of Rides"
)
ride_week %>%
ggplot(aes(weekday,avg_trip,fill=user)) +
geom_col(position = "dodge") +
scale_y_continuous(labels=comma) +
labs(
title = "Average Trip Time by Day of a Week",
subtitle = "Average trip time for each day of a week by user",
caption = "Figure 5",
x = "Days of a Week",
y = "Average Trip Time"
)
ride_week %>%
ggplot(aes(weekday,total_trip,fill=user)) +
geom_col(position = "dodge") +
scale_y_continuous(labels=comma) +
labs(
title = "Total Trip Time by Day of a Week",
subtitle = "Total trip time for each day of a week by user",
caption = "Figure 6",
x = "Days of a Week",
y = "Total Trip Time"
)
ride_month = analyse_table %>%
group_by(user, month) %>%
dplyr::summarise(no_of_rides = n(),
avg_trip = mean(trip_time),
total_trip = sum(trip_time)
)
ride_month %>%
ggplot(aes(month, no_of_rides, fill=user)) +
geom_col(position="dodge") +
scale_y_continuous(labels=comma) +
labs(
title = "No. of Rides by Month of the Year",
subtitle = "Number of rides on each month by users",
caption = "Figure 7",
x = "Month of the Year",
y = "Number of Rides"
)
ride_month %>%
ggplot(aes(month, avg_trip, fill=user)) +
geom_col(position = "dodge") +
scale_y_continuous(labels=comma) +
labs(
title = "Average Trip Time by Month of a Year",
subtitle = "Average trip time for each month of the year by user",
caption = "Figure 8",
x = "Month of the Year",
y = "Average Trip Time"
)
ride_month %>%
ggplot(aes(month, total_trip, fill=user)) +
geom_col(position = "dodge") +
scale_y_continuous(labels=comma) +
labs(
title = "Total Trip Time by Month of a Year",
subtitle = "Total trip time for each month of a year by user",
caption = "Figure 9",
x = "Month of a Year",
y = "Total Trip Time"
)
ride_bike = analyse_table %>%
group_by(user, bike) %>%
dplyr::summarise(no_of_rides = n(),
avg_trip = mean(trip_time),
total_trip = sum(trip_time)
)
ride_bike %>%
ggplot(aes(bike, no_of_rides, fill = user))+
geom_col(position = "dodge")+
scale_y_continuous(labels = comma)+
labs(
title = "Number of Trips per Bike Type and Segregated by User",
subtitle = "Number of trips per bike type",
caption = "Fig 10",
x = "Types of Bikes",
y = "Number of Trips"
)
ride_bike %>%
ggplot(aes(bike, avg_trip, fill = user))+
geom_col(position = "dodge")+
scale_y_continuous(labels = comma)+
labs(
title = "Average Trip Time per Bike Type and Segregated by User",
subtitle = "Average trip time per bike type",
caption = "Fig 12",
x = "Type of Bikes",
y = "Avg Trip Time"
)
ride_bike %>%
ggplot(aes(bike, total_trip, fill = user))+
geom_col(position = "dodge")+
scale_y_continuous(labels = comma)+
labs(
title = "Total Trip Time per Bike Type and Segregated by User",
subtitle = "Total trip time per bike type",
caption = "Fig 13",
x = "Type of Bikes",
y = "Total Trip Time"
)