forked from YouSayData/DSR_Intermediate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_solutions.R
72 lines (61 loc) · 1.57 KB
/
functions_solutions.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
# 1
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE, finite = TRUE)
result <- (x - rng[1]) / (rng[2] - rng[1])
result[which(is.infinite(result) & result > 0)] <- 1
result[which(is.infinite(result) & result < 0)] <- 0
result
}
x <- c(Inf, -Inf, 1, 0, 0.3)
rescale01(x)
# 2
both_na <- function(x,y) {
which(is.na(x) & is.na(y))
}
nanana <- c(1,2,NA,3,5,NA,6,NA)
batman <- c(NA,2,NA,3,5,NA,NA,NA)
both_na(nanana, batman)
# 3
bootleFunc <- function(start = 99, drink = "beer", vessel = "bottle") {
if (start < 1) {
return("You have no drinks left.")
}
new <- start - 1
if (start == 1) {
return(str_c(start, vessel,
"of",
drink,
"on the wall,",
start, vessel,
"of",
drink,
"If this",
vessel,
"happens to fall,",
"no",
str_c(vessel, "s"),
"of",
drink,
"on the wall.",
sep = " "
))
}
if (start == 2) {
return(str_c(start,
"bottles of beer on the wall,",
start,
"bottles of beer. If one of those bottles happens to fall",
new,
"bottle of beer on the wall.",
sep = " "
))
}
str_c(start,
"bottles of beer on the wall,",
start,
"bottles of beer. If one of those bottles happens to fall",
new,
"bottles of beer on the wall.",
sep = " "
)
}