-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotes.Rmd
329 lines (236 loc) · 4.46 KB
/
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
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
---
title: "Stat 33A - Lecture 2"
date: February 3, 2020
output: pdf_document
---
## Announcements
First homework assignment posted to the bCourse.
## Using Functions, Part 3
Last time we saw how to call a function and set the arguments.
```{r}
sum(1, 2, 3)
```
```{r}
x = 1
```
Each argument gets assigned to a _parameter_, which is like a variable inside
of the function.
Parameters are listed in each function's help file:
```{r}
?log
```
You can set arguments based on the order of the parameters:
```{r}
log(10, 2) # log of 10, base 2
```
Or by using the names of the parameters:
```{r}
log(x = 10, base = 2)
```
```{r}
log(base = 2, 10)
```
```{r}
log(bas = 2, x = 10)
```
## Data Types & Classes
In statistics, we categorize data into different types:
* Continuous (real numbers)
* Discrete (integers, or finite number of values)
* Logical (1 and 0s, T and Fs)
* Nominal (categorical values with no ordering)
* Ordinal (categorical values with ordering)
* Graph (network data)
* Textual (books, websites, etc)
In R, we categorize values into different classes.
Every value has at least one class.
Class answers the question "How does this thing behave?"
Check class with `class()`:
```{r}
class(5)
```
```{r}
class("hi")
```
```{r}
class(TRUE)
```
```{r}
class(cos)
```
```{r}
x = 1
class(x)
```
R also has something called _types_. Not as important as classes for day-to-day
programming.
## Special Values
R has four special values.
`NA` represents a missing or unknown value in a data set.
```{r}
NA
```
```{r}
class(NA)
```
The missing value `NA` is contagious!
```{r}
5 + NA
```
Using a unknown argument in a computation usually produces an unknown result.
`NULL` represents a value that's not defined _in R_. Functions often use `NULL` to mean the absence of a result.
```{r}
dim(5)
```
```{r}
class(NULL)
```
For instance, if we try to get the matrix dimensions of a vector:
```{r}
```
`NaN`, or "not a number", represents a value that's not defined mathematically.
Produced by some computations:
```{r}
0 / 0
```
```{r}
class(NaN)
```
`Inf` represents infinity. Produced by some computations:
```{r}
- 5 / 0
```
```{r}
class(Inf)
```
## Comparison Operators
We saw operators for doing arithmetic: `+`, `-`, `*`, `/`, `^`
R also has operators for making comparisons.
Use `==` to check equality:
```{r}
1 == 2
```
```{r}
NA == 1
```
If you want to check equality to `NA`, use `is.na()` instead:
```{r}
is.na(NA)
```
There are `is.` functions for the other special values, too.
```{r}
is.infinite(Inf)
```
Check inequality with `<`, `<=`, `>`, and `>=`. For instance:
```{r}
4 <= 5
```
## Vectors
A vector is an ordered collection of values.
In R, all of the values in a vector must be the same data type.
Several ways to create a vector. One is the `c()` function, which __c__ombines
values:
```{r}
c(1, 2, 3)
```
```{r}
c("hi", "hello", '33A')
```
Check length with `length()`:
```{r}
length(c(1, 2, 3))
```
```{r}
length(6)
```
```{r}
class(c(1, 2, 3))
```
```{r}
x = c(1, 7, 9, 8)
```
Another way is to create a sequence with `:` or `seq()`:
```{r}
1:5
```
More ways are described in this week's reading.
```{r}
7:20
```
```{r}
?seq
seq(1, 5, 0.5)
```
R does not make any distinction between scalars and vectors.
A scalar is just a vector with one element.
```{r}
```
You can name elements of a vector:
```{r}
# Can use quotes to use numbers as names, but not recommended
x = c("dogs" = 3, "2" = 4)
```
```{r}
x = c(dogs2 = 3, cats2 = 4)
```
```{r}
names(x)
```
To set set names, use:
```{r}
names(x) = c("a", "b")
```
```{r}
names(x)
```
Many operations are _vectorized_, which means they are computed
element-by-element. For instance:
```{r}
c(1, 2, 3) + c(4, 5, 6)
```
```{r}
sin(c(1, 2, 3))
```
```{r}
c(1, 2) == c(3, 2)
```
```{r}
c(1, 2) + c(3, 4, 5)
```
```{r}
c(1, 2, 3) + 1
```
What happens if we pass several different data types to `c()`?
```{r}
class(c(7, "hi", TRUE))
```
## Implicit Coercion
R can automatically convert between ("coerce") types in one direction:
logical -> integer -> numeric -> complex -> character
```{r}
class(1 + 4i)
```
```{r}
1 + 0i
```
For instance:
```{r}
5 + TRUE
```
So if we pass different types to `c()`, R will attempt coercion:
```{r}
c(TRUE, 2.0)
```
```{r}
class(c(1, sin))
```
There are data types R will never implicitly coerce:
```{r}
class(sin)
```
## Lists
In a vector, every element has to have the same type.
A list is a container for elements with *different* types.
```{r}
list(1, sin, "hi")
```