-
Notifications
You must be signed in to change notification settings - Fork 0
/
case_bike.Rmd
351 lines (261 loc) · 8.58 KB
/
case_bike.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
---
title: "R Notebook"
output: html_notebook
---
# Case Study 1: How Does a Bike-Share Navigate Speedy Success?
## Introduction
Welcome to the Cyclistic bike-share analysis case study! In this case study, you will perform many real-world tasks of a junior data analyst.
You will work for a fictional company, Cyclist, and meet different characters and team members. In order to answer the key business questions, you will follow the steps of the data analysis process: ask, prepare, process, analyze, share, and act. Along the way, the Case Study Roadmap tables --- including guiding questions and key tasks --- will help you stay on the right path.
By the end of this lesson, you will have a portfolio-ready case study. Download the packet and reference the details of this case study anytime. Then, when you begin your job hunt, your case study will be a tangible way to demonstrate your knowledge and skills to potential employers.
## Scenario
You are a junior data analyst working in the marketing analyst team at Cyclistic, a bike-share company in Chicago. The director of marketing believes the company's future success depends on maximizing the number of annual memberships. Therefore, your team wants to understand how casual riders and annual members use Cyclistic bikes differently.
From these insights, your team will design a new marketing strategy to convert casual riders into annual members. But first, Cyclistic executives must approve your recommendations, so they must be backed up with compelling data insights and professional data visualizations.
## Ask
Three questions will guide the future marketing program:
> 1\. How do annual members and casual riders use **Cyclistic** bikes differently?
>
> 2\. Why would casual riders buy **Cyclistic** annual memberships?
>
> 3\. How can **Cyclistic** use digital media to influence casual riders to become members?
## Prepare
```{r}
install.packages("geosphere")
install.packages("modeest")
install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr") # alternative installation of the %>%
```
```{r}
library(tidyverse) # for data wrangling
library(lubridate) # for wrangling data attributes
library(ggplot2) # for visualizations
library(dplyr)
library(geosphere)
```
### Load data
```{r}
library(readr)
```
```{r}
getwd()
```
```{r}
cyclist_raw_04 <- read_csv("case_study_da_bike/ciclyst_csv/202004-divvy-tripdata.csv")
```
```{r}
cyclist_raw_05 <- read_csv("case_study_da_bike/ciclyst_csv/202005-divvy-tripdata.csv")
```
```{r}
cyclist_raw_06 <- read_csv("case_study_da_bike/ciclyst_csv/202006-divvy-tripdata.csv")
```
```{r}
cyclist_raw_07 <- read_csv("case_study_da_bike/ciclyst_csv/202007-divvy-tripdata.csv")
```
```{r}
cyclist_raw_08 <- read_csv("case_study_da_bike/ciclyst_csv/202008-divvy-tripdata.csv")
```
```{r}
cyclist_raw_09 <- read_csv("case_study_da_bike/ciclyst_csv/202009-divvy-tripdata.csv")
```
```{r}
cyclist_raw_10 <- read_csv("case_study_da_bike/ciclyst_csv/202010-divvy-tripdata.csv")
```
```{r}
cyclist_raw_11 <- read_csv("case_study_da_bike/ciclyst_csv/202011-divvy-tripdata.csv")
```
```{r}
cyclist_raw_12 <- read_csv("case_study_da_bike/ciclyst_csv/202012-divvy-tripdata.csv")
```
```{r}
cyclist_raw_01 <- read_csv("case_study_da_bike/ciclyst_csv/202101-divvy-tripdata.csv")
```
```{r}
cyclist_raw_02 <- read_csv("case_study_da_bike/ciclyst_csv/202102-divvy-tripdata.csv")
```
```{r}
cyclist_raw_03 <- read_csv("case_study_da_bike/ciclyst_csv/202103-divvy-tripdata.csv")
```
## Merge
```{r}
cyclist_raw <- rbind(
cyclist_raw_04,
cyclist_raw_05,
cyclist_raw_06,
cyclist_raw_07,
cyclist_raw_08,
cyclist_raw_09,
cyclist_raw_10,
cyclist_raw_11,
cyclist_raw_12,
cyclist_raw_01,
cyclist_raw_02,
cyclist_raw_03,
deparse.level = 1
)
```
```{r}
cyclist_raw
```
```{r}
??rbin
```
## Process
- [x] Open your spreadsheet and create a column called "ride_length."
- [ ] Calculate the length of each ride by subtracting the column "started_at" from the column "ended_at" (for example, =D2-C2) and format as HH:MM:SS using Format \> Cells \> Time \> 37:30:55.
```{r}
cyclist_mod <- cyclist_raw
cyclist_mod
```
```{r}
cyclist_mod_clear_na <- na.omit(cyclist_mod) # Clear NA in the dataset (-5.6 %)
cyclist_mod_clear_na
```
```{r}
cyclist_mod_row_na = cyclist_mod[rowSums(is.na(cyclist_mod)) > 0, ] #(5.6 %)
cyclist_mod_row_na
```
```{r}
#cyclist_mod$ride_length <- cyclist_mod$ended_at - cyclist_mod$started_at
```
```{r}
interv_mod <-interval(cyclist_mod$started_at, cyclist_mod$ended_at)
ride_length_mod <- as.duration(interv_mod)
#ride_length
```
```{r}
#as.numeric(ride_length, "minutes")
#as.numeric(ride_length, "hours")
```
```{r}
class(cyclist_mod$ended_at )
```
Note: Description of the classes `"POSIXlt"-> (year, month, day)` and `"POSIXct"-> (hour, min, sec)` representing calendar dates and times (to the nearest second).
```{r}
cyclist_mod$ride_length <- ride_length_mod
```
```{r}
cyclist_mod
```
```{r}
cyclist_mod_neg_ride_length <- cyclist_mod[cyclist_mod$ride_length < 0,]
cyclist_mod_neg_ride_length
```
```{r}
# 60 seg
cyclist_mod_min_ride_length <- cyclist_mod[cyclist_mod$ride_length > 60,]
cyclist_mod_min_ride_length
```
```{r}
# 30000 secs
cyclist_mod_long_ride <- cyclist_mod[cyclist_mod$ride_length > 30000 ,]
cyclist_mod_long_ride
```
```{r}
#cyclist_mod <- cyclist_mod[cyclist_mod$ride_length < 30000 ,]
```
```{r}
start <- cbind(cyclist_mod_clear_na$start_lng, cyclist_mod_clear_na$start_lat)
end <- cbind(cyclist_mod_clear_na$end_lng, cyclist_mod_clear_na$end_lat)
```
```{r}
library(sp)
```
```{r}
pts_start <- SpatialPoints(start)
pts_end <- SpatialPoints(end)
```
```{r}
dist_short_station <- distHaversine(pts_start, pts_end, r= 6378137)
```
```{r}
cyclist_mod_clear_na$dist_short_between_stations <- dist_short_station
```
- [ ] Create a column called "day_of_week," and calculate the day of the week that each ride started using the "WEEKDAY" command (for example, =WEEKDAY(C2,1)) in each file. Format as General or as a number with no decimals, noting that 1 = Sunday and 7 = Saturday
```{r}
# 1 = Sunday and 7 = Saturday
day_of_week <- wday(cyclist_mod_clear_na$started_at)
```
```{r}
cyclist_mod_clear_na$day_of_week <- day_of_week
```
```{r}
cyclist_mod_clear_na
```
## Analyze
4\. Run a few calculations in one file to get a better sense of the data layout. Options:
> ● Calculate the mean of ride_length
>
> ● Calculate the max ride_length
>
> ● Calculate the mode of day_of_week
```{r}
summary(cyclist_mod)
```
```{r}
max(cyclist_mod$ride_length)
```
```{r}
min(cyclist_mod$ride_length)
```
```{r}
mean(cyclist_mod$ride_length)
```
```{r}
library(modeest)
```
```{r}
mlv(cyclist_mod$day_of_week, method = "mfv")
```
Create a pivot table to quickly calculate and visualize the data. Options:
● Calculate the average ride_length for members and casual riders. Try rows = member_casual; Values = Average of ride_length.
● Calculate the average ride_length for users by day_of_week. Try columns = day_of_week; Rows = member_casual; Values = Average of ride_length.
● Calculate the number of rides for users by day_of_week by adding Count of trip_id to Values.
```{r}
aggregate(cyclist_mod$ride_length, list(cyclist_mod$member_casual), mean)
```
```{r}
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr) # alternatively, this also loads %>%
```
```{r}
cyclist_mod %>%
group_by(member_casual) %>%
summarise_at(vars(ride_length), funs(mean(., na.rm=TRUE)))
```
```{r}
day_ride_length <- cyclist_mod %>%
group_by(day_of_week, member_casual) %>%
summarise_at(vars(ride_length ), funs(mean(., na.rm=TRUE)))
```
```{r}
day_ride_length
```
```{r}
cyclist_long_ride %>%
group_by(day_of_week) %>%
summarise(ride_id) %>%
count()
```
```{r}
test <- cyclist_mod %>%
mutate(weekday = wday(started_at, label = TRUE)) %>% #creates weekday field using wday()
group_by(member_casual, weekday) %>% #groups by usertype and weekday
summarise(number_of_rides = n() #calculates the number of rides and average duration
,average_duration = mean(ride_length)) %>% # calculates the average duration
arrange(member_casual, weekday) # sorts
```
```{r}
View(test)
```
```{r}
cyclist_long_ride %>%
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 = number_of_rides, fill = member_casual)) +
geom_col(position = "dodge")
```
```{r}
plot(cyclist_mod$ride_length~cyclist_mod$dist_short_station)
```