-
Notifications
You must be signed in to change notification settings - Fork 10
/
lec_04_exercises.R
47 lines (32 loc) · 956 Bytes
/
lec_04_exercises.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
# Exercise 1 - scope
## What is the output of the following code? Explain why.
z = 1
f = function(x, y, z) {
z = x+y
g = function(m = x, n = y) {
m/z + n/z
}
z * g()
}
f(1, 2, x = 3)
# Exercise 2 - classes, modes, and types
## Below we have defined an S3 method called `report`, it is designed to return a message about the type/mode/class of an object passed to it.
report = function(x) {
UseMethod("report")
}
report.default = function(x) {
"This class does not have a method defined."
}
report.integer = function(x) {
"I'm an integer!"
}
report.double = function(x) {
"I'm a double!"
}
report.numeric = function(x) {
"I'm a numeric!"
}
## Try running the `report` function with different input types, what happens? <br/>
## Now run `rm("report.integer")` in your console and try using the `report` <br/>
## function again, what has changed? What does this tell us about S3, types, modes, <br/>
## and classes?