-
Notifications
You must be signed in to change notification settings - Fork 1
/
workshop 2.R
293 lines (174 loc) · 4.34 KB
/
workshop 2.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
290
291
# Workshop 2
# Intro stuff ####----------------------------------------
library(tidyverse)
#install.packages("nycflights13")
library(nycflights13)
1 / 200 * 30
(59 + 73 + 2) / 3
sin(pi / 2)
?sin
x <- 3 * 4
x
y <- 5
z <- 3
# i_use_snake_case
# otherPeopleUseCamelCase
# some.people.use.periods
# And_janE_kaY_RENOUNCES.Convention
this_is_a_really_long_name <- 2.5
this_is_a_really_long_name
r_rocks <- 2 ^ 3
r_rock
R_rocks
# function_name(argument1 = val1, argument2 = val2, ...)
seq(1,10)
x <- "Hello world"
#x
my_variable <- 10
my_varlable
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
filter(mpg, cyl == 8)
filter(diamonds, carat > 3)
diamonds
# three variable ggplot
# www.link.i.used.com/answer
# Data transformation ####--------------------------------------
flights
?flights
view(flights)
flights <- flights
# dplyr
filter()
arrange()
select()
mutate()
summarise()
group_by()
jan_1 <- filter(flights, month == 1, day == 1)
dec_25 <- filter(flights, month == 12, day == 25)
dec_25
filter(flights, month == 1)
sqrt(2) ^ 2 == 2
near(sqrt(2) ^ 2, 2)
# & AND
# | OR
# ! NOT
filter(flights, month == 11 | month == 12)
#filter(flights, month == (11 | 12)) #not what we expected (or hoped for)
filter(flights, month %in% c(11,12))
filter(flights, !(arr_delay > 120 | dep_delay > 120))
filter_1 <- filter(flights, arr_delay <= 120 & dep_delay <= 120)
filter_2 <- filter(flights, arr_delay <= 120, dep_delay <= 120)
identical(filter_1, filter_2)
NA > 5
10 == NA
NA + 10
NA / 2
NA == NA
x <- NA
y <- NA
x == y
is.na(x)
df <- tibble(x = c(1, NA, 3))
df
filter(df, x > 1)
filter(df, is.na(x) | x > 1)
#ex 5.2.4
#1
filter(flights, arr_delay >= 120)
#2
filter(flights, dest == "IAH" | dest == "HOU")
#3
unique(flights$carrier)
filter(flights, carrier %in% c("UA", "AA", "DL"))
#4
filter(flights, month %in% c(7,8,9))
filter(flights, month %in% c(7:9))
?between()
filter(flights, between(month, 7,9))
# arrange
arrange(flights, year, month, day)
arrange(flights, desc(dep_delay))
# Ex 5.3.1
# q1
df <- tibble(x = c(1, NA, 3))
df
arrange(df, x)
arrange(df, desc(x))
arrange(df, desc(is.na(x)))
#q2
arrange(flights, desc(arr_delay))
#earlist in the day
arrange(flights, dep_time)
#earliest compared to planned departure
arrange(flights, dep_delay)
#Select
select(flights, year, month, day)
select(flights, year:day)
select(flights, -(year:day))
# starts_with("test")
# ends_with("xyz")
# contains("blah")
# matches("(.)\\1") #Regular expression
flights <- rename(flights, tail_num = tailnum)
select(flights, tail_num, everything())
# ex 5.4.1
#q2
select(flights, month, year, month)
#q4
vars <- c("year", "month", "day", "dep_delay", "arr_delay")
vars
select(flights, any_of(vars))
#select(other_dataset, any_of(vars))
select(flights, contains("TIME"))
flights
?select
select
#mutate
flights_sml <- select(flights,
year:day,
ends_with("delay"),
distance,
air_time)
mutate(flights_sml,
gain = dep_delay - arr_delay,
speed = distance / air_time * 60)
mutate(flights_sml,
gain = dep_delay - arr_delay,
speed = distance / air_time * 60,
hours = air_time / 60,
gain_per_hour = gain / hours)
x <- 1:10
x
lag(x)
dplyr::lead(x)
cumsum(x)
# grouped summaries
summarise(flights, delay = mean(dep_delay, na.rm = TRUE))
by_day <- group_by(flights, year, month, day)
summarise(by_day, delay = mean(dep_delay, na.rm = TRUE))
#copy from text
#grouping
by_dest <- group_by(flights, dest)
#summarising
delay <- summarise(by_dest,
count = n(),
dist = mean(distance, na.rm = TRUE),
delay = mean(arr_delay, na.rm = TRUE)
)
#> `summarise()` ungrouping output (override with `.groups` argument)
# filtering
delay <- filter(delay, count > 20, dest != "HNL")
# It looks like delays increase with distance up to ~750 miles
# and then decrease. Maybe as flights get longer there's more
# ability to make up delays in the air?
ggplot(data = delay, mapping = aes(x = dist, y = delay)) +
geom_point(aes(size = count), alpha = 1/3) +
geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
delays <- flights %>%
group_by(dest) %>%
summarise(count = n()
) %>%
filter(count >20, dest != "HNL")