-
Notifications
You must be signed in to change notification settings - Fork 1
/
workshop 8.R
360 lines (254 loc) · 6.08 KB
/
workshop 8.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
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
352
353
354
355
356
357
358
359
360
# Workshop 8
#pipes ####----------------------------------------
# %>%
library(magrittr)
# Little bunny Foo Foo
# Went hopping through the forest
# Scooping up the field mice
# And bopping them on the head
foo_foo <- little_bunny()
hop()
scoop()
bop()
# 4 ways to code
#1 save each intermediate step as new object
#2 overwrite original object many times
#3 compose functions
#4 ues the pipe
#1 save intermediate
foo_foo_1 <- hop(foo_foo, through = forest)
foo_foo_2 <- scoop(foo_foo_1, up = field_mice)
foo_foo_3 <- bop(foo_foo_2, on = head)
#2 overwrite
foo_foo <- hop(foo_foo, through = forest)
foo_foo <- scoop(foo_foo, up = field_mice)
foo_foo <- bop(foo_foo, on = head)
#3 function
bop(
scoop(
hop(foo_foo, through = forest),
up = field_mice
),
on = head
)
#4 pipe
foo_foo %>%
hop(through = forest) %>%
scoop(up = field_mice) %>%
bop(on = head)
# Functions ####------------------------------------------
df <- tibble::tibble(
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)
df
df$a <- (df$a - min(df$a, na.rm = TRUE)) /
(max(df$a, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$b <- (df$b - min(df$b, na.rm = TRUE)) /
(max(df$b, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$c <- (df$c - min(df$c, na.rm = TRUE)) /
(max(df$c, na.rm = TRUE) - min(df$c, na.rm = TRUE))
df$d <- (df$d - min(df$d, na.rm = TRUE)) /
(max(df$d, na.rm = TRUE) - min(df$d, na.rm = TRUE))
df$a <- (df$a - min(df$a, na.rm = TRUE)) /
(max(df$a, na.rm = TRUE) - min(df$a, na.rm = TRUE))
x <- df$a
(x - min(x, na.rm = TRUE)) /
(max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
rng <- range(x, na.rm = TRUE)
rng
rng[1] #min
rng[2] #max
(x - rng[1]) / (rng[2] - rng[1])
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE, finite = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
rescale01(df$a)
rescale01(df$b)
# 3 key steps to making a function
#1 name for the function
#2 arguments (data, other arguments)
#3 code that you stick inside the function
rescale01(c(-10, 0, 10))
rescale01(c(-10, 0, 10, NA))
rescale01(c(-10, 0, 10, NA, Inf))
#Ex19.2.1
#q2
x
mean(is.na(x))
prop_na <- function(x) {
mean(is.na(x))
}
prop_na(x)
y <- c(3,5,NA)
prop_na(y)
x / sum(x, na.rm = TRUE)
sd(x, na.rm = TRUE) / mean(x, na.rm = TRUE)
?sd
# function names
f()
my_awesome_function()
impute_missing()
imp_mis()
input_select()
input_checkbox()
input_text()
select_input()
checkbox_input()
library(tidyverse)
str_
#don't do this
# T <- FALSE
# mean <- function(x) {sum(x)}
# Ex 19.3.1
#q1
check_prefix <- function(string, prefix) {
substr(string, 1, nchar(prefix)) == prefix
}
?substr
check_prefix("testing_this", "test")
check_prefix("testing_this", "bollocks")
# Conditional execution
if(condition) {
# code to execute if true
} else {
# code to execute when condition is false
}
# conditions
if (c(TRUE, FALSE)) {}
if (NA) {}
# if with Floating points not exactly equal, use near()
#multiple conditions
if (this) {
#do that
} else if (that) {
#do seomething else
} else {
#third remaining alternative
}
switch()
cut()
?cut
#Ex 19.4.4
#q1
?`if`
?ifelse
?if_else
#q2
# Write a greeting function that says “good morning”,
# “good afternoon”, or “good evening”, depending on the time of day.
# (Hint: use a time argument that defaults to lubridate::now(). That will make it easier to test your function.)
# name is greeting
# Argument is time (date_time)
# code
library(lubridate)
morning_example <- ymd_hms("20200201 10:30:00")
morning_example
afternoon_example <- ymd_hms("20200201 12:01:00")
afternoon_example
evening_example <- ymd_hms("20200201 20:01:00")
evening_example
date_times <- as_tibble(c(morning_example, afternoon_example, evening_example))
date_times
hour(morning_example) < 12
"good morning"
# hour(morning_example) < 17
# "good afternoon"
# else
# "good evening"
greeting <- function(x) {
if (hour(x) < 12) {
return("good morning")
} else if (hour(x) < 17) {
return("good afternoon")
} else {
return("good evening")
}
}
?if_else
greeting(morning_example)
greeting(afternoon_example)
greeting(evening_example)
greeting(NA)
#Function arguments
log() #data x, detail base 2, n , e?
mean() #data x, detail na.rm True or false
str_c() # data multiple strings, detail sep
conf_int <- function(x, conf = 0.05)
hollys_function <- function(x, na.rm = TRUE) {}
aes(x = mpg, y = eff)
#checking values
wt_mean <- function(x, w) {
if ( length(x) != length(w)) {
stop("x is different length - you dum dum, give me gum gum")
}
sum(x * w) / sum(w)
}
data <- 1:6
data
weights <- 1:3
weights
wt_mean(data, weights)
#stopifnot # check multiple conditions all at once
# dot dot dot
sum(1, 2, 3, 45, 678, 1)
#...
x <- c(1,2)
x
sum(x, na.mr = TRUE)
sum(x, na.rm = TRUE)
# return values
# pipeable functions
show_missings <- function(df) {
n <- sum(is.na(df))
cat("Missing values: ", n, sep = "")
invisible(df)
}
mtcars %>%
show_missings()
library(nycflights13)
flights %>%
show_missings()
#environment
toy_function <- function(x) {
x + y
}
# lexical scoping
toy_function(10)
df
toy_function(df$a)
# Stacey's question, is "greeting" function going to calculate ####---------------
# for each value in the vector provided?
# i.e., if vector supplied has 3 objects, will output have 3 outputs?
library(tidyverse)
library(lubridate)
morning_example <- ymd_hms("20200201 10:30:00")
morning_example
afternoon_example <- ymd_hms("20200201 12:01:00")
afternoon_example
evening_example <- ymd_hms("20200201 20:01:00")
evening_example
date_times <- as_tibble(c(morning_example, afternoon_example, evening_example))
date_times
#greeting function
greeting <- function(x) {
if (hour(x) < 12) {
return("good morning")
} else if (hour(x) < 17) {
return("good afternoon")
} else {
return("good evening")
}
}
#does it work?
greeting(date_times$value) # this is not vectorised, uses only first value
greeting_vectorised <- function(x) {
if_else( (hour(x) < 12), "good morning",
if_else( (hour(x) < 17), "good afternoon", "good evening")
)
}
?if_else
greeting_vectorised(date_times$value) #three outputs