-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshifts_intervals.R
134 lines (103 loc) · 4.31 KB
/
shifts_intervals.R
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
load("../Rdata/one_month_taxi.Rdata")
library(tidyr)
library(dplyr)
###########################################
#examine shifts, active hours, and downtime
###########################################
taxi_clean_shifts <- taxi_clean %>% group_by(hack_license) %>%
arrange(pickup_datetime) %>%
mutate(downtime = as.numeric(difftime(lead(pickup_datetime),
dropoff_datetime,
units = "hours")))
###################################
#Creating the is_end_shift function
##################################
taxi_clean_shifts <- taxi_clean_shifts %>% filter(!is.na(downtime))
is_end_shift_function = function(num)
{
if ( num >= 6)
{
1
}
else
{
0
}
}
is_end_shift_function = Vectorize(is_end_shift_function)
taxi_clean_shifts <- taxi_clean_shifts %>%
mutate(is_end_shift = is_end_shift_function(downtime))
############################################
#Creating another col for the is_start_shift
############################################
taxi_clean_shifts <- taxi_clean_shifts %>%
mutate(is_start_shift = lag(is_end_shift, default = 1))
taxi_clean_shifts <- taxi_clean_shifts %>%
mutate(shift_num = cumsum(is_start_shift))
############################################
#replace NA's in borough, neighborhood info
############################################
out_of_bounds = "Outside NYC"
out_of_bounds_code = 0
# Cleaning pickup borough
taxi_clean_shifts$pickup_borough =
as.character(taxi_clean_shifts$pickup_borough)
taxi_clean_shifts$pickup_borough =
replace(taxi_clean_shifts$pickup_borough,
is.na(taxi_clean_shifts$pickup_borough), out_of_bounds)
taxi_clean_shifts$pickup_borough =
as.factor(taxi_clean_shifts$pickup_borough)
# Cleaning pickup borough codes
taxi_clean_shifts$pickup_boroughCode =
as.numeric(taxi_clean_shifts$pickup_boroughCode)
taxi_clean_shifts$pickup_boroughCode =
replace(taxi_clean_shifts$pickup_boroughCode,
is.na(taxi_clean_shifts$pickup_boroughCode), out_of_bounds_code)
taxi_clean_shifts$pickup_boroughCode =
as.factor(taxi_clean_shifts$pickup_boroughCode)
# Cleaning dropoff borough
taxi_clean_shifts$dropoff_borough =
as.character(taxi_clean_shifts$dropoff_borough)
taxi_clean_shifts$dropoff_borough =
replace(taxi_clean_shifts$dropoff_borough,
is.na(taxi_clean_shifts$dropoff_borough), out_of_bounds)
taxi_clean_shifts$dropoff_borough =
as.factor(taxi_clean_shifts$dropoff_borough)
# Cleaning dropoff borough codes
taxi_clean_shifts$dropoff_boroughCode =
as.numeric(taxi_clean_shifts$dropoff_boroughCode)
taxi_clean_shifts$dropoff_boroughCode =
replace(taxi_clean_shifts$dropoff_boroughCode,
is.na(taxi_clean_shifts$dropoff_boroughCode), out_of_bounds_code)
taxi_clean_shifts$dropoff_boroughCode = as.factor(taxi_clean_shifts$dropoff_boroughCode)
# Cleaning up dropoff and pickup neighborhoods
taxi_clean_shifts$pickup_neighborhood =
as.character(taxi_clean_shifts$pickup_neighborhood)
taxi_clean_shifts$pickup_neighborhood =
replace(taxi_clean_shifts$pickup_neighborhood,
is.na(taxi_clean_shifts$pickup_neighborhood), out_of_bounds)
taxi_clean_shifts$pickup_neighborhood =
as.factor(taxi_clean_shifts$pickup_neighborhood)
taxi_clean_shifts$dropoff_neighborhood =
as.character(taxi_clean_shifts$dropoff_neighborhood)
taxi_clean_shifts$dropoff_neighborhood =
replace(taxi_clean_shifts$dropoff_neighborhood,
is.na(taxi_clean_shifts$dropoff_neighborhood), out_of_bounds)
taxi_clean_shifts$dropoff_neighborhood =
as.factor(taxi_clean_shifts$dropoff_neighborhood)
###############################
#Added fare and shift length
###############################
shifts_clean <- taxi_clean_shifts %>% group_by(hack_license, shift_num) %>%
summarize(start_shift = first(pickup_datetime),
end_shift = last(dropoff_datetime),
fare= sum(fare_amount),
active_hours =length(unique(c(pickup_hour, dropoff_hour))) ,
total_trips = n(),
total_distance = sum(trip_distance),
total_duration_in_seconds = sum(trip_time_in_secs)
) %>%
mutate(shift_length = as.numeric(difftime(end_shift,
start_shift,
units = "hours")))
save(shifts_clean, taxi_clean_shifts, file = "../Rdata/shifts.Rdata")