-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataManipulation.R
289 lines (225 loc) · 8.59 KB
/
DataManipulation.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
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
# install.pacakges('dplyr')
library(stats)
library(base)
library(dplyr)
# setwd("SET THE Working Director to THE PATH TO THIS DIRECTORY")
DelayDataLocation <- "./Datasets/airline/HoustonAirline.csv"
delay.dat.houston <- read.csv(DelayDataLocation,
header=TRUE,
stringsAsFactors = FALSE)
# tbl_df allows for nice printing
delay.dat.houston <- tbl_df(delay.dat.houston)
###
delay.dat.houston
# Airport information
AirDataLocation <- "./Datasets/airline/airports.csv"
airport.dat <- read.table(AirDataLocation,
header=TRUE,
sep=",",
stringsAsFactors = FALSE)
airport.dat <- tbl_df(airport.dat)
###
airport.dat
# Find all flight which occurred in Janurary
filter(delay.dat.houston, Month==1)
# we could of course save this too
# delay.dat.houston.jan <- fitler(delay.dat.houston, Month==1)
## ---- filterEx2 -----------------------------------------------
# Using airport data, find a list of iata abbreviations for houston texas airports
filter(airport.dat, state=='TX', city=='Houston')
## ---- filterEx3 ------------------------------------------------
# Find the subset of flight departing from Hobby Airport "HOU" for which the Actual
# Elapsed Time was greater than the CRS Elapsed Time.
filter(delay.dat.houston,
Origin == 'HOU', # iata code for Hobby
ActualElapsedTime > CRSElapsedTime)
## ---- filterEx4 -----------------------------------------------
# Find the subset of flights departing on the weekend.
filter(delay.dat.houston, DayOfWeek == 6 | DayOfWeek == 7)
# another alternative
filter(delay.dat.houston, DayOfWeek %in% c(6,7))
## ---- arrangeEx1 ----------------------------------------------
# arrange, like filter, operates on data.frame rows
# arrange is used for sorting data.frame rows w.r.t. a given column(s)
# sort by DayofMonth, smallest to largest
arrange(delay.dat.houston, DayofMonth)
## ---- arrangeEx2 ----------------------------------------------
# sort by DayofMonth largest to smallest
arrange(delay.dat.houston, desc(DayofMonth))
## ---- arrangeEx3 ----------------------------------------------
# sort by Month, use DayofMonth to break ties
arrange(delay.dat.houston, desc(Month), desc(DayofMonth))
## ---- selectEx1 ------------------------------------------------
select(delay.dat.houston, Year, Month, DayofMonth)
select(delay.dat.houston, Year:DayofMonth)
select(delay.dat.houston, -(Year:DayofMonth))
## ---- helperlist----------------------------------------------
## # will give list of helper functions
?select
## ---- helperex ----------------------------------------------
# search for text string/regular expression
select(delay.dat.houston, contains('Dep'))
## ---- helperex2 -----------------------------------------------
select(delay.dat.houston,
one_of('UniqueCarrier',
'FlightNum'))
select(delay.dat.houston,
ends_with('Time'))
## ---- distinctEx1 ---------------------------------------------
# returns a data.frame with 12 observations
distinct(delay.dat.houston, Month)
distinct(delay.dat.houston, Month,.keep_all=TRUE)
## ---- distinctEx2 ---------------------------------------------
# returns a data.frame with 12*7=84 observations
distinct(delay.dat.houston, Month,DayOfWeek)
select(
distinct(
arrange(
filter(delay.dat.houston,DayOfWeek==6),
desc(ActualElapsedTime)),
UniqueCarrier,.keep_all = TRUE),
UniqueCarrier, ActualElapsedTime)
delay.dat.houston %>%
filter(DayOfWeek == 6) %>%
arrange(desc(ActualElapsedTime)) %>%
distinct(UniqueCarrier,.keep_all=TRUE) %>%
select(UniqueCarrier,ActualElapsedTime)
## ---- ChainSols1 -----------------------------------------------
# Find a list of the distinct Origin airports
delay.dat.houston %>%
distinct(Origin)
# Find a list of distinct (Origin, Dest) pairs
delay.dat.houston %>%
distinct(Origin, Dest)
# Origin airport with largest Janurary departure delay
delay.dat.houston %>%
filter(Month==1) %>%
arrange(desc(DepDelay)) %>%
select(Month,Origin, DepDelay) %>%
distinct(Origin,.keep_all = TRUE)
## ---- ChainSols2 -----------------------------------------------
# largest departure delay for each carrier for each month
delay.dat.houston %>%
arrange(Month,desc(DepDelay)) %>%
select(Month,UniqueCarrier,DepDelay) %>%
distinct(Month,UniqueCarrier,.keep_all=TRUE)
## ---- mutateexs ------------------------------------------------
# create new variable ElapsedDifference:
delay.dat.houston %>% mutate(
ElapsedDiffernce = ActualElapsedTime - CRSElapsedTime)
# keep only the newly created variable:
delay.dat.houston %>% transmute(
ElapsedDiffernce = ActualElapsedTime - CRSElapsedTime)
## ---- summariseEx1 --------------------------------------------------------
# Basic example with no grouping
delay.dat.houston %>%
summarise(
MeanDistance = mean(Distance,na.rm=TRUE)
)
# Results identical to transmutate. boring.
## ---- summariseEx2 . --------------------------------------------
# With grouping
# n() is dplyr function counts # obs in each group
delay.dat.houston %>%
group_by(UniqueCarrier) %>%
summarise(
MeanDistance=mean(Distance,na.rm=TRUE),
NFlights = n())
## ---- summex3 -------------------------------------------------------------
delay.dat.houston %>%
group_by(Month, UniqueCarrier) %>%
summarise(MaxDepDelay = max(DepDelay,na.rm=TRUE)) %>%
head(5)
## ---- plotdley -------------------------------------------------
library(ggplot2)
delay.dat.houston %>%
group_by(Month,UniqueCarrier) %>%
summarise(
Dep = mean(DepDelay,na.rm=TRUE)
) -> tmp
qplot(Month,Dep,data=tmp) +
geom_line() +
facet_wrap(~UniqueCarrier)
## ---- plotsize2 ------------------------------------------------
delay.dat.houston %>%
group_by(Month,UniqueCarrier) %>%
summarise(
NFlights = n()
) -> tmp
qplot(Month,NFlights,data=tmp) +
geom_line() +
facet_wrap(~UniqueCarrier,scale='free_y')
## ---- scatter_explot -------------------------------
delay.dat.houston %>%
group_by(UniqueCarrier) %>%
summarise(
NFlights = n(),
NCancelled = sum(Cancelled)) %>%
mutate(
PercentCancelled = (NCancelled/NFlights)*100) %>%
select(UniqueCarrier,
PercentCancelled)
## ---- nuthers ------------------------
delay.dat.houston %>%
group_by(UniqueCarrier) %>%
summarise(
Dep = mean(DepDelay,na.rm=TRUE),
Arr = mean(ArrDelay,na.rm=TRUE),
NFlights = n()
) %>%
select(Dep,Arr,NFlights) -> tmp
qplot(Dep,
Arr,
data=tmp,
size=log(NFlights))+
geom_abline(intercept=0,slope=1,color='red')
## ---- merge_toy_read . ----------------------------
people.info <- read.table('./data/mergedata/PeopleInfo.csv',
sep=',',
header=TRUE)
occup.info <- read.table('./data/mergedata/OccupationInfo.csv',
sep=',',
header=TRUE)
## ---- look_at_toy ---------------------------------------------------------
people.info
occup.info
## ---- join_try1 ------------------------------------------------
# What do you think the following snippets will do
# Try to guess before running, then run to confirm
left_join(people.info, occup.info)
right_join(people.info, occup.info)
inner_join(people.info, occup.info)
# Do the following return the same data set?
left_join(people.info, occup.info)
right_join(occup.info, people.info)
# Do you think this will work?
people.info %>% left_join(occup.info)
## ---- weird_joins ----------------------------------------------
semi_join(people.info, occup.info)
anti_join(people.info, occup.info)
full_join(people.info, occup.info)
## ---- mergit ---------------------------------------------------
delay.dat.houston %>%
left_join(airport.dat,
by=c("Dest" = 'iata'))
## ---- plotmregereal ------------------
delay.dat.houston %>%
left_join(airport.dat,
by=c("Dest" = 'iata')) %>%
group_by(state) %>%
summarise(
NFlights = n()
) %>%
select(state,NFlights)
## ---- mregeplots25 ---------------------------------------------
# one option
delay.dat.houston %>%
left_join(airport.dat,
by=c("Dest" = 'iata')) %>%
group_by(UniqueCarrier, state) %>%
summarise(
AvgDelay = mean(DepDelay,na.rm=TRUE)
) %>%
select(state,UniqueCarrier, AvgDelay) %>%
arrange(UniqueCarrier, desc(AvgDelay)) %>%
distinct(UniqueCarrier,.keep_all = TRUE)