-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblank_notes.Rmd
234 lines (134 loc) · 4.14 KB
/
blank_notes.Rmd
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
---
title: "Stat 33A - Lecture Notes 10"
date: Oct 25, 2020
output: pdf_document
---
Functions
=========
A function is like a factory: raw materials (inputs) go in, products (outputs)
come out.
```
+-------+
-- in -->| f |-- out -->
+-------+
```
For a function:
* _Parameters_ are placeholder variables for inputs.
+ _Arguments_ are actual values assigned to the parameters in a call.
* The _return value_ is the output.
* The _body_ is the code inside.
You can write your own function with the `function` command.
The syntax is:
```
function(parameter1, parameter2, ...) {
# Your code goes here
# The result on the last line is automatically returned
}
```
For example, let's make a function that detects negative numbers:
```{r}
```
Generally, you should:
* Assign the function to a variable.
+ Exception: _anonymous functions_ in calls to apply functions
+ Functions are actions, so use a verb in the name.
* Indent the body of the function by 2 or 4 spaces.
As another example, let's make a function that detects leap years:
```{r}
```
Curly braces are optional if the body is only one line:
```{r}
```
To exit the function and return a result early, use `return()`.
It only makes sense to use `return()` inside an if-statement!
For example, suppose we want to return `NA` if the argument isn't a number:
```{r}
```
It's idiomatic to only use `return()` when strictly necessary.
To return multiple values, use a vector or list.
For example, let's make a function that computes the mean and median for a
vector:
```{r}
```
Default Arguments
=================
Parameters can have default arguments that are used if no argument is provided.
Use `=` (not `<-`) to assign default arguments:
```{r}
```
Function Example
================
Variables: Scope & Lookup
=======================
Local Variables
---------------
A variable's *scope* is the section of code where it exists and is accessible.
The `exists()` function checks whether a variable is in scope:
```{r}
```
When you create a function, you create a new scope.
Variables defined inside of a function are *local* to the function.
Local variables cannot be accessed from outside:
```{r}
```
Local variables are reset each time the function is called:
```{r}
```
The `local()` function also creates a new scope:
```{r}
```
Rarely used in practice.
Lexical Scoping
---------------
A function can use variables defined outside (non-local), but only if those
variables are in scope where the function was **defined**.
This property is called *lexical scoping*.
For example:
```{r}
```
Variables defined directly in the R console are *global* and available to any
function.
Local variables *mask* (hide) non-local variables with the same name:
```{r}
```
ONE EXCEPTION!
We often call variables:
```{r}
```
In this case, the variable must refer to a function, so R ignores local
variables that aren't.
For example:
```{r}
```
Dynamic Lookup
--------------
Variable lookup happens when a function is **called**, not when it's defined.
This is called *dynamic lookup*.
For example:
```{r}
```
Summary
-------
* Function definitions (or `local()`) create a new scope.
* Local variables
+ Are private
+ Get reset for each call
+ Mask non-local variables (exception: function calls)
* *Lexical scoping*: where a function is **defined** determines which non-local
variables are in scope.
* *Dynamic lookup*: when a function is **called** determines values of
non-local variables.
Using Functions
===============
Functions are the building blocks for solving problems.
Take a divide-and-conquer approach, breaking large problems into
smaller steps.
Use a short function for each step. Then it's easier to:
* Test that each step works correctly.
* Modify, reuse, or repurpose a step.
Before you write a function, it's useful to go through several steps:
1. Write down what you want to do, in detail. It can also help to
draw a picture of what needs to happen.
2. Check whether there's already a built-in function. Google it!
3. Write the code to handle a simple case first. For data science
problems, use a small dataset at this step.