-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson1_slides.qmd
588 lines (391 loc) · 10.2 KB
/
lesson1_slides.qmd
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
---
title: "W1: Vectors, data.frames and lists"
format:
live-revealjs:
df-print: paged
smaller: true
scrollable: true
echo: true
embed-resources: true
drop:
engine: webr
button: true
output-location: fragment
---
{{< include ./_extensions/r-wasm/live/_knitr.qmd >}}
## Welcome!
![](images/Intro_To_R_2.png){width="300"} ![](images/Intermediate_R_draft.png){width="375"}
```{webr}
#| edit: false
#| echo: false
library(tidyverse)
library(palmerpenguins)
```
## Introductions
- Who am I?
- TA: Monica Gerber - in-class resource
. . .
- What is DaSL?
. . .
- Who are you?
- Name, pronouns, group you work in
- What you want to get out of the class
- Favorite spring activity
## Goals of the course
. . .
- Continue building **programming fundamentals**: *How to use complex data structures, use and create custom functions, and how to iterate repeated tasks using tools in the `tidyverse`.*
. . .
- Continue exploration of **data science fundamentals**: *How to clean messy data to a Tidy form for analysis using tools in the `tidyverse`.*
. . .
- At the end of the course, you will be able to: conduct a full analysis in the data science workflow (minus model).
![Data science workflow](https://d33wubrfki0l68.cloudfront.net/571b056757d68e6df81a3e3853f54d3c76ad6efc/32d37/diagrams/data-science.png){width="550"}
## Culture of the course
. . .
- Learning on the job is challenging
- I will move at learner's pace; we are learning together.
- Teach not for mastery, but teach for empowerment to learn effectively.
. . .
- Various personal goals and applications: curate applications based on your interest!
. . .
- Respect Code of Conduct
## Format of the course
. . .
- 6 classes: Jan 22, 29, Feb. 5, 12, 26, Mar 6
- **No class during Public School Week**
. . .
- Streamed online and in person, recordings will be available.
- Announcements via Teams Classroom and by Google Doc
. . .
- 1-2 hour exercises after each session are strongly encouraged as they provide practice.
- Optional time to work on exercises together on Fridays Noon - 1pm PT.
. . .
- Online discussion via Slack.
## Content of the course
|Week|Date|Subject|
|----|----|-------|
|1|Jan 22*|Fundamentals: vectors, data.frames, and lists|
|2|Jan 29|Data Cleaning 1|
|3|Feb 5|Data Cleaning 2|
|4|Feb 12*|Writing Functions|
|-|Feb 19|No class - school week|
|5|Feb 26*|Iterating/Repeating Tasks|
|6|Mar 6*|Overflow/Celebratory Lunch|
*Ted on Campus
## Office Hours
- Opportunity to Practice & ask questions
- 10 - 11 AM PST Fridays
- Outlook link will be shared
# Ask me two questions
# Set up Posit Cloud and look at our workspace!
## Break
A pre-course survey:
https://forms.gle/aLXyQor4WS5mTKMV6
## Note
- We'll do exercises live in the slides
- they are mirrored in your workspaces as `classwork`
- Exercises in your projects
## Data types in R
- Numeric: 18, -21, 65, 1.25
. . .
- Character: "ATCG", "Whatever", "948-293-0000"
. . .
- Logical: TRUE, FALSE
. . .
- Missing values: `NA`
## Data structures in R
- Vector
- Dataframe
- List
## Vectors
A **vector** contains a data type, and all elements must be the same data type. We can have **logical vectors, numerical vectors**, etc.
![](https://d33wubrfki0l68.cloudfront.net/eb6730b841e32292d9ff36b33a590e24b6221f43/57192/diagrams/vectors/summary-tree-atomic.png){width="300"}
. . .
Within the Numeric type that we are familiar with, there are more specific types: **Integer vectors** consists of whole number values, and **Double vectors** consists of decimal values.
. . .
```{r}
fib = c(0, 1, 1, NA, 5)
```
## Testing and Coercing
We can test whether a vector is a certain type with `is.___()` functions, such as `is.character()`.
```{r}
is.character(c("hello", "there"))
```
. . .
For `NA`, the test will return a vector testing each element, because `NA` can be mixed into other values:
```{r}
is.na(c(34, NA))
```
. . .
We can **coerce** vectors from one type to the other with `as.___()` functions, such as `as.numeric()`
```{r}
as.numeric(c("23", "45"))
```
. . .
```{r}
as.numeric(c(TRUE, FALSE))
```
## Attributes of data structures
It is common to have metadata **attributes**, such as **names**, attached to R data structures.
```{r}
x = c(1, 2, 3)
names(x) = c("a", "b", "c")
x
```
. . .
![](https://d33wubrfki0l68.cloudfront.net/1140c34226b3b04438aec65c8fc6b28758d8c091/1748a/diagrams/vectors/attr-names-2.png)
. . .
```{r}
x["a"]
```
. . .
We can look for more general attributes via the `attributes()` function:
```{r}
attributes(x)
```
## Ways to subset a vector
```{r}
data = c(2, 4, -1, -3, 2, -1, 10)
```
. . .
1. Positive numeric vector
```{r}
data[c(1, 2, 7)]
```
. . .
2. Negative numeric vector performs *exclusion*
```{r}
data[-1]
```
. . .
3. Logical vector
```{r}
data[c(TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE)]
```
. . .
Comparison operators, such as `>`, `<=`, `==`, `!=`, create logical vectors for subsetting.
```{r}
data < 0
```
. . .
```{r}
data[data < 0]
```
## Try it Out: Vectors 1
1. How do you subset the following vector so that it only has positive values?
```{webr}
#| exercise: v_1
data = c(2, 4, -1, -3, 2, -1, 10)
data[data -- 0]
```
::: {.solution exercise="v_1"}
#### Solution
```{webr}
#| exercise: v_1
#| solution: true
data = c(2, 4, -1, -3, 2, -1, 10)
data[data > 0]
```
:::
## Vectors 2
2. How do you subset the following vector so that it has doesn't have the character "temp"?
```{webr}
#| exercise: v_2
chars = c("temp", "object", "temp", "wish", "bumblebee", "temp")
chars[chars -- "temp"]
```
::: {.solution exercise="v_2"}
#### Solution
```{webr}
#| exercise: v_2
#| solution: true
chars = c("temp", "object", "temp", "wish", "bumblebee", "temp")
chars[chars != "temp"]
```
:::
## Vectors 3
3. Challenge: How do you subset the following vector so that it has no `NA` values?
```{webr}
#| exercise: v_3
vec_with_NA = c(2, 4, NA, NA, 3, NA)
vec_with_NA[!----(vec_with_NA)]
```
::: {.solution exercise="v_3"}
#### Solution
```{webr}
#| exercise: v_3
#| solution: true
vec_with_NA = c(2, 4, NA, NA, 3, NA)
vec_with_NA[!is.na(vec_with_NA)]
```
:::
## Dataframes
Usually, we load in a dataframe from a spreadsheet or a package.
```{r, message=F, warning=F}
library(tidyverse)
library(palmerpenguins)
head(penguins)
```
. . .
Let's take a look at a dataframe's **attributes**.
```{r, message=F, warning=F}
attributes(penguins)
```
. . .
So, we can access the column names of the dataframe via `names()`:
```{r}
names(penguins)
```
## Try it out: Subsetting dataframes 1
*Subset to the single column `bill_length_mm`:*
```{webr}
#| exercise: df_1
penguins
```
::: {.solution exercise="df_1"}
#### Solution
```{webr}
#| exercise: df_1
#| solution: true
penguins$bill_length_mm
# or
penguins[["bill_length_mm"]]
```
:::
## Subsetting dataframes 2
*I want to select columns `bill_length_mm`, `bill_depth_mm`, `species`, and filter the rows so that `species` only has "Gentoo":*
```{webr}
#| exercise: df_2
penguins |>
select( ) |>
filter( )
```
::: {.solution exercise="df_2"}
#### Solution
```{webr}
#| exercise: df_2
#| solution: true
penguins |>
select(bill_length_mm, bill_depth_mm, species) |>
filter(species == "Gentoo")
```
:::
## Subsetting dataframes 3
*Challenge: I want to filter out rows that have `NA`s in the column `bill_length_mm`:*
```{webr}
#| exercise: df_3
penguins |>
filter(!----(bill_length_mm))
```
::: {.solution exercise="df_3"}
#### Solution
```{webr}
#| exercise: df_3
#| solution: true
penguins |>
filter(!is.na(bill_length_mm))
```
:::
## Lists
Lists operate similarly as vectors as they group data into one dimension, but each element of a list can be any data type *or data structure*!
```{r}
l1 = list(
1:3,
"a",
c(TRUE, FALSE, TRUE),
c(2.3, 5.9)
)
```
. . .
![](https://d33wubrfki0l68.cloudfront.net/9628eed602df6fd55d9bced4fba0a5a85d93db8a/36c16/diagrams/vectors/list.png)
. . .
Unlike vectors, you access the elements of a list via the double bracket `[[]]`. (You will access a smaller list with single bracket `[]`.)
```{r}
l1[[1]]
```
. . .
```{r}
l1[[1]][2]
```
## List names
We can give **names** to lists:
```{r}
l1 = list(
ranking = 1:3,
name = "a",
success = c(TRUE, FALSE, TRUE),
score = c(2.3, 5.9)
)
#or
names(l1) = c("ranking", "name", "success", "score")
```
## Accessing List elements
And access named elements of lists via the `[[]]` or `$` operation:
```{r}
l1[["score"]]
l1$score
```
. . .
Therefore, `l1$score` is the same as `l1[[4]]` and is the same as `l1[["score"]]`.
. . .
What data structure does this remind you of?
## Warning: `[]` versus `[[]]`
This always trips me up, you usually want `[[]]` (return an element) versus `[]` (returns a sublist).
```{r}
l1["ranking"]
```
```{r}
l1[["ranking"]]
```
## Two main uses for Lists
1. Return a mixed type list of objects, such as from running `lm()` - a lot of methods in R use this.
- Useful when programming functions
2. Store multiple instances of the same data type, such as a `list` of `data.frame`s
- Iteration over lists
## Try it Out:
Return the element in the `id` slot:
```{webr}
#| exercise: list_1
person = list(id=100031, age=40)
person
```
::: {.solution exercise="list_1"}
```{webr}
#| exercise: list_1
#| solution: true
person = list(id=100031, age=40)
person$id
person[["id"]]
```
:::
Return the 2nd element of this list:
```{webr}
#| exercise: list_2
new_list <- list(c(1,2,3), c(3,4,5), c(5,7,8))
new_list
```
::: {.solution exercise="list_2"}
```{webr}
#| exercise: list_2
#| solution: true
new_list <- list(c(1,2,3), c(3,4,5), c(5,7,8))
new_list[[2]]
```
:::
## Dataframes as Lists
A dataframe is just a named list of vectors of same length with **attributes** of (column) `names` and `row.names`, so all of the list methods we looked at above apply.
. . .
```{r}
head(penguins)
```
. . .
```{r}
head(penguins[[3]])
head(penguins$bill_length_mm)
head(penguins[["bill_length_mm"]])
```
# Everything in R is a List, or based on one
## Tidyverse tools for lists
- `lapply()` function - applies a function to each element of a list
- We'll explore in Week 5 the `{purrr}` package, which has methods for working with lists
## That's all!
Maybe see you Friday 10 - 11 AM PST to practice together!