-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
145 lines (122 loc) · 2.55 KB
/
functions.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
library(tidyverse)
diamonds %>%
select(carat, price)
# simple function
add_two_numbers <- function(x, y) {
x + y
}
add_two_numbers(1, 4)
added <- add_two_numbers(1, 4)
added
add_two_numbers <- function(x, y) {
return(x + y)
}
added <- add_two_numbers(1, 4)
added
math_two_numbers <- function(x, y, operation) {
if (operation == "add") {
return(x + y)
}
return(x - y)
}
math_two_numbers(1, 4, "add")
math_two_numbers(1, 4, "asdfihsadkfhkadjfhkdjsfksdjk")
math_two_numbers <- function(x, y, operation) {
if (operation == "add") {
return(x + y)
} else if (operation == "subtract") {
return(x - y)
} else {
stop("I don't know what operation you passed.")
}
}
math_two_numbers(1, 4, "add")
math_two_numbers(1, 4, "asdfihsadkfhkadjfhkdjsfksdjk")
math_two_numbers(1, 4, "subtract")
math_two_numbers <- function(x, y, operation) {
if (operation == "add") {
result <- x + y
}
if (operation == "subtract") {
result <- x - y
}
return(result)
}
math_two_numbers_default <- function(x, y, operation = "add") {
if (operation == "add") {
return(x + y)
} else if (operation == "subtract") {
return(x - y)
} else {
stop("I don't know what operation you passed.")
}
}
math_two_numbers_default(1, 4, "add")
math_two_numbers_default(1, 4, "asdfihsadkfhkadjfhkdjsfksdjk")
math_two_numbers_default(1, 4, "subtract")
math_two_numbers_default(1, 4)
add_two_numbers <- function(x, y, operation = "add") {
if (operation == "add") {
return(x + y)
} else if (operation == "subtract") {
return(x - y)
} else {
stop("I don't know what operation you passed.")
}
}
# lexical scoping: name masking
x <- 10
add_to_x <- function(to_add) {
x <- 5
return(x + to_add)
}
add_to_x(100)
x
# lexical scoping: dynamic lookup
x <- 5
add_to_x <- function(to_add) {
return(x + to_add)
}
add_to_x(100)
add_to_x <- function(to_add, x) {
return(x + to_add)
}
x <- 5
add_to_x(100, x)
# functions: fresh start
x <- 10
add_to_x <- function(to_add) {
x <- to_add + x
return(x)
}
add_to_x(2)
add_to_x(2)
add_to_x(2)
add_to_x(2)
f2c <- function(temp) {
return((temp - 32) * 5 / 9)
}
f2c(32)
f2c("32")
f2c("missing")
f2c <- function(temp) {
if (!is.numeric(temp)) {
stop("Please pass me a number.")
}
return((temp - 32) * 5 / 9)
}
f2c(32)
f2c(212)
f2c("32")
f2c("missing")
f2c <- function(temp) {
if (is.numeric(temp)) {
return((temp - 32) * 5 / 9)
}
return(stop("i didn't get a number"))
}
library(testthat)
expect_equal(f2c(32), 0)
expect_equal(f2c(212), 100)
#expect_equal(f2c(32), 1)
expect_error(f2c("42"))