Skip to content

Commit 20cf9e0

Browse files
committed
Add week 3.
1 parent 7e7ae5d commit 20cf9e0

File tree

3 files changed

+901
-0
lines changed

3 files changed

+901
-0
lines changed

Diff for: lecture/09.06/blank_notes.Rmd

+307
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
---
2+
title: "Stat 33A - Lecture Notes 3"
3+
date: September 6, 2020
4+
output: pdf_document
5+
---
6+
7+
Data Types
8+
==========
9+
10+
In statistics, we categorize data into different types:
11+
12+
* Continuous (real numbers)
13+
* Discrete (integers, or finite number of values)
14+
* Logical (1 and 0s, T and Fs)
15+
* Nominal (categorical values with no ordering)
16+
* Ordinal (categorical values with ordering)
17+
* Graph (network data)
18+
* Textual (books, websites, etc)
19+
20+
R also categorizes data into different types.
21+
22+
23+
Type specifies how the object is stored in memory.
24+
25+
Type answers the question "What is this thing?"
26+
27+
The `typeof()` function returns an object's type:
28+
```{r}
29+
30+
```
31+
32+
33+
## Classes
34+
35+
Every object also has at least one class.
36+
37+
Class answers the question "How does this thing behave?"
38+
39+
The `class()` function returns an object's class:
40+
```{r}
41+
42+
```
43+
44+
Classes are more important than types for day-to-day programming.
45+
46+
47+
## Identifying Vectors
48+
49+
Technically, class and type are independent.
50+
51+
For vectors, usually the class and type are the same:
52+
```{r}
53+
54+
```
55+
56+
Vectors of decimal numbers are an exception:
57+
```{r}
58+
59+
```
60+
61+
## Implicit Coercion
62+
63+
R can automatically convert or **coerce** types in one direction:
64+
65+
logical -> integer -> numeric -> complex -> character
66+
67+
68+
For example:
69+
```{r}
70+
71+
```
72+
73+
This process is called **implicit coercion**.
74+
75+
76+
We already saw this for the `c()` function:
77+
```{r}
78+
79+
```
80+
81+
There are data types R will never implicitly coerce:
82+
```{r}
83+
84+
```
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
Matrices, Arrays, & Lists
99+
=========================
100+
101+
## Matrices & Arrays
102+
103+
**Matrices** are two-dimensional containers for values.
104+
105+
You can create a matrix from a vector with the `matrix()` function:
106+
```{r}
107+
108+
```
109+
110+
The elements of a matrix must all have the same type.
111+
112+
It's usually faster to operate on matrix columns rather than rows.
113+
114+
The matrix multiplication operator is `%*%`:
115+
```{r}
116+
117+
```
118+
119+
120+
**Arrays** generalize vectors and matrices to higher dimensions.
121+
122+
Use the `array()` function to create an array:
123+
```{r}
124+
125+
```
126+
127+
128+
## Lists
129+
130+
131+
A **list** is a container for elements with _different_ types.
132+
133+
The `c()` function returns a list if implicit coercion is not possible:
134+
```{r}
135+
136+
```
137+
138+
You can also create a list with the `list()` function:
139+
```{r}
140+
141+
```
142+
143+
List elements can have names:
144+
```{r}
145+
146+
```
147+
148+
Vectorized functions don't work for lists:
149+
```{r}
150+
151+
```
152+
153+
Access list elements by position with `[[`, the extraction operator:
154+
```{r}
155+
156+
```
157+
158+
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
169+
170+
171+
172+
173+
174+
Special Values
175+
==============
176+
177+
R has four special values.
178+
179+
180+
## Missing Values
181+
182+
`NA` represents a **missing value** in a data set:
183+
```{r}
184+
185+
```
186+
187+
The missing value `NA` is contagious!
188+
```{r}
189+
190+
```
191+
192+
Using a unknown argument in a computation usually produces an unknown result.
193+
194+
195+
## Null
196+
197+
`NULL` represents a value that's not defined _in R_.
198+
199+
`NULL` usually indicates absence of a result:
200+
```{r}
201+
202+
```
203+
204+
For instance, if we try to get the matrix dimensions of a vector:
205+
```{r}
206+
207+
```
208+
209+
210+
## Not a Number
211+
212+
`NaN`, or "not a number", represents a value that's not defined mathematically.
213+
214+
215+
```{r}
216+
217+
```
218+
219+
220+
# Infinite Values
221+
222+
`Inf` represents infinity. Produced by some computations:
223+
224+
```{r}
225+
226+
```
227+
228+
229+
230+
231+
232+
233+
234+
235+
236+
237+
238+
239+
240+
241+
242+
243+
244+
245+
246+
247+
248+
Making Comparisons
249+
==================
250+
251+
We saw operators for doing arithmetic: `+`, `-`, `*`, `/`, `^`
252+
253+
R also has operators for making comparisons:
254+
255+
* `==` for equality
256+
* `<`, `<=`, `>`, `>=` for inequality
257+
258+
For example:
259+
```{r}
260+
261+
```
262+
263+
Comparison operators are vectorized, just like arithmetic operators:
264+
```{r}
265+
266+
```
267+
268+
To check equality for special values, use the `is.` functions:
269+
270+
* `is.na()`
271+
* `is.null()`
272+
* `is.nan()`
273+
* `is.infinite()`
274+
275+
For example:
276+
```{r}
277+
278+
```
279+
280+
281+
## Equality & Numerical Precision
282+
283+
To check equality within a tolerance, use `all.equal()`:
284+
```{r}
285+
286+
# isTRUE
287+
288+
```
289+
290+
Two reasons to use `all.equal()`:
291+
292+
1. Sometimes you want a tolerance (e.g., simulations).
293+
294+
2. On computers, all numbers are discrete (so most are imprecise).
295+
296+
As an example of the second point:
297+
```{r}
298+
299+
```
300+
301+
302+
To check whether two objects are identical, use `identical()`.
303+
304+
The `identical()` function is _not_ vectorized:
305+
```{r}
306+
307+
```

0 commit comments

Comments
 (0)