-
Notifications
You must be signed in to change notification settings - Fork 24
/
01-class_03_data-basics.Rmd
346 lines (258 loc) Β· 6.22 KB
/
01-class_03_data-basics.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
---
title: "Data basics"
author: "Andrew Heiss"
date: "August 23, 2021"
output:
xaringan::moon_reader:
lib_dir: "libs"
chakra: "libs/remark-latest.min.js"
css: ["default", "css/ath-slides.css", "css/ath-inferno-fonts.css"]
seal: false
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
ratio: "16:9"
navigation:
scroll: false
# beforeInit: "libs/offline-mathjax.js"
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE,
fig.retina = 3, fig.align = "center")
```
```{r packages-data, include=FALSE}
library(tidyverse)
library(gapminder)
library(countdown)
library(flair)
library(knitr)
library(kableExtra)
```
```{r xaringanExtra, echo=FALSE}
xaringanExtra::use_xaringan_extra(c("tile_view"))
```
class: center middle main-title section-title-6
# Data basics
---
class: middle section-title-inv-6
.box-6.large[Packages]
---
<figure>
<img src="img/01-class/02/packages-base.png" alt="R packages, base" title="R packages, base" width="100%">
</figure>
---
<figure>
<img src="img/01-class/02/packages-packages.png" alt="R packages, other" title="R packages, other" width="100%">
</figure>
---
class: title title-6
# Using packages
.pull-left[
```{r eval=FALSE}
install.packages("name")
```
.box-inv-6[Downloads files<br>to your computer]
.box-inv-6[Do this once per computer]
]
--
.pull-right[
```{r eval=FALSE}
library("name")
```
.box-inv-6[Loads the package]
.box-inv-6[Do this once per R session]
]
---
class: title title-6
# The tidyverse
.pull-left[
.box-inv-6.small["The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures."]
.box-inv-6[β¦ the tidyverse makes data science faster, easier and more funβ¦]
]
.pull-right[
<figure>
<img src="img/01-class/02/tidyverse.png" alt="The tidyverse" title="The tidyverse" width="100%">
</figure>
]
---
class: title title-6
# The tidyverse
<figure>
<img src="img/01-class/02/tidyverse-language.png" alt="tidyverse and language" title="tidyverse and language" width="100%">
</figure>
???
From "Master the Tidyverse" by RStudio
---
class: title title-6
# The tidyverse package
.center[
```{r eval=FALSE}
library(tidyverse)
```
]
.box-inv-6[The tidyverse package is a shortcut for<br>installing and loading all the key tidyverse packages]
---
.pull-left[
```{r eval=FALSE}
install.packages("tidyverse")
```
.tiny[
```{r eval=FALSE}
install.packages("ggplot2")
install.packages("dplyr")
install.packages("tidyr")
install.packages("readr")
install.packages("purrr")
install.packages("tibble")
install.packages("stringr")
install.packages("forcats")
install.packages("lubridate")
install.packages("hms")
install.packages("DBI")
install.packages("haven")
install.packages("httr")
install.packages("jsonlite")
install.packages("readxl")
install.packages("rvest")
install.packages("xml2")
install.packages("modelr")
install.packages("broom")
```
]
]
--
.pull-right[
```{r eval=FALSE}
library("tidyverse")
```
.tiny[
```{r eval=FALSE}
library("ggplot2")
library("dplyr")
library("tidyr")
library("readr")
library("purrr")
library("tibble")
library("stringr")
library("forcats")
```
]
]
---
class: middle section-title-inv-6
.box-6.large[Rectangular data]
---
class: title title-6
# Data frames and tibbles
.box-inv-6.medium[Data frames are the most common kind of data objects; used for rectangular data (like spreadsheets)]
--
.box-6[Data frames: R's native data object]
--
.box-6[Tibbles (`tbl`): a fancier enhanced kind of data frame]
--
.box-6.small[(You really won't notice a difference today)]
---
class: middle section-title-inv-6
.box-6.large[Vectors and lists]
---
class: title title-6
# Vectors
.box-inv-6[Vectors are a list of values of the same time<br>(all text, or all numbers, etc.)]
.box-inv-6[Make them with `c()`:]
```{r eval=FALSE}
c(1, 4, 2, 5, 7)
```
--
.box-inv-6[You'll usually want to assign them to something:]
```{r eval=FALSE}
neat_numbers <- c(1, 4, 2, 5, 7)
```
---
class: title title-6
# Basic data types
<table>
<tr>
<td><b>Integer</b></td>
<td>Whole numbers</td>
<td><code class="remark-inline-code">c(1, 2, 3, 4)</code></td>
</tr>
<tr>
<td><b>Double</b></td>
<td>Numbers</td>
<td><code class="remark-inline-code">c(1, 2.4, 3.14, 4)</code></td>
</tr>
<tr>
<td><b>Character</b></td>
<td>Text</td>
<td><code class="remark-inline-code">c("1", "blue", "fun", "monster")</code></td>
</tr>
<tr>
<td><b>Logical</b></td>
<td>True or false</td>
<td><code class="remark-inline-code">c(TRUE, FALSE, TRUE, FALSE)</code></td>
</tr>
<tr>
<td><b>Factor</b></td>
<td>Category</td>
<td><code class="remark-inline-code">c("Strongly disagree", "Agree", "Neutral")</code></td>
</tr>
</table>
---
class: middle section-title-inv-6
.box-6.large[Importing data]
---
class: title title-6
# Packages for importing data
<table>
<tr>
<td><img src="img/01-class/02/readr.png" alt="readr" title="readr" width="150px"></td>
<td>Work with plain text data</td>
<td><code class="remark-inline-code">my_data <- read_csv("file.csv")</code></td>
</tr>
<tr>
<td><img src="img/01-class/02/readxl.png" alt="readxl" title="readxl" width="150px"></td>
<td>Work with Excel files</td>
<td><code class="remark-inline-code">my_data <- read_excel("file.xlsx")</code></td>
</tr>
<tr>
<td><img src="img/01-class/02/haven.png" alt="haven" title="haven" width="150px"></td>
<td>Work with Stata, SPSS, and SAS data</td>
<td><code class="remark-inline-code">my_data <- read_stata("file.dta")</code></td>
</tr>
</table>
---
class: title title-6
# Other types of data
<table>
<tr>
<th>Package</th>
<th>Type of data</th>
</tr>
<tr>
<td>jsonlite</td>
<td>JSON data</td>
</tr>
<tr>
<td>xml2</td>
<td>XML data</td>
</tr>
<tr>
<td>httr</td>
<td>Web APIs</td>
</tr>
<tr>
<td>rvest</td>
<td>Web pages (web scraping)</td>
</tr>
<tr>
<td>DBI</td>
<td>Databases (SQL)</td>
</tr>
</table>
---
class: title title-6
# Next up
.box-inv-6.medium[Data visualization]