-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotes.Rmd
415 lines (274 loc) · 8.07 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
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
---
title: "Stat 33B - Lecture 4"
date: February 12, 2020
output: pdf_document
---
Announcements
=============
Working with Data
=================
We'll need the dogs data again:
```{r}
dogs = readRDS("data/dogs/dogs_full.rds")
```
Taking Subsets
--------------
Taking subsets is a fundamental operation for working with data in R!
Two primary operators for taking subsets in R:
1. `[[` to "extract" an element.
+ Selects exactly one element.
+ Drops the container.
+ By position or name.
2. `[` to "subset" an element.
+ Selects any number of elements.
+ Keeps the container.
+ By position, name, or logical values.
Let's test these out on a vector:
```{r}
x = c(7, -3, 1)
```
Subsetting by position (positions start at 1 in R):
```{r}
x[1]
x[[1]]
x[c(1, 2, 2)]
x[[c(1, 2, 2)]]
```
Negative positions mean "everything except":
```{r}
x[-2]
x[[-1]] # can't return multiple elements
```
Subsetting by name:
```{r}
x
y = c(a = 7, b = -5, c = 1)
y
y["a"]
x["a"] # no element named "a"
y[["b"]]
x[["b"]] # error
y[c("a", "b", "a")]
```
Subsetting by logical value:
```{r}
y > 0
which(y > 0)
y[which(y > 0)] # subsetting by position
y[y > 0]
# One case where this breaks:
y_withNA = c(7, -1, NA)
y_withNA > 0
y_withNA[y_withNA > 0]
y_withNA[which(y_withNA > 0)] # drops the NAs
```
Using `which()` means an extra call, so extra time!
```{r}
x[[c(FALSE, FALSE, TRUE)]]
```
```{r}
# If length of logical doesn't match, R recycles:
x
# FALSE TRUE FALSE TRUE FALSE TRUE ...
x[c(FALSE, TRUE)]
```
`[[` drops the container, `[` does not.
This is most apparent for lists:
```{r}
x = list(a = c(6, 5), b = "hi")
x
class(x[1])
class(x[[1]])
x
x[1]
x[[1]]
x[[c(2, 2)]]
x[[2]]
x[[c(1, 2)]] # recursive subsetting: 1st element's 2nd element
x[[1]][[2]] # equivalent to above line
x[[1]]
x[1][1][1][1] # keeps the container
x[c(1, 1, 2)]
```
A visual representation of this idea:
https://twitter.com/hadleywickham/status/643381054758363136
Data frames are lists, so:
```{r}
class(dogs[1])
dogs[[1]]
```
We can also do multi-dimensional subsetting:
```{r}
head(dogs)
dogs[1, 1] # drops the container
# NOTE: Data frames with class "tbl" and "tbl_df" are called tibbles, and part
# of a Tidyverse package called tibble.
#
# Notice that the dogs data frame is a tibble.
#
# In recent versions of the tibble package, using `[` to subset a single value
# DOES NOT drop the data frame.
#
# So you may see different behavior above if you have a recent version of the
# tibble package.
#
# If you want to see the behavior for ordinary data frames, try subsetting a
# non-tibble data frame, such as R's built-in `iris` data frame:
iris[1, 1]
# Thanks to the student that pointed out the behavior of `[` is now different
# for tibbles.
#
# You can also force R to drop the data frame with the `drop` parameter:
dogs[1, 1, drop = TRUE]
# The `[[` operator ALWAYS drops the container:
dogs[[1, 1]]
# If you select multiple elements, `[` always keeps the container:
dogs[1, 1:3] # row 1, columns 1 through 3
```
Use a blank to mean "everything" along one dimension:
```{r}
dogs[1, ] # first row, all columns
```
Two more functions for taking subsets:
1. `$` extracts an element by name. Tries to match partial names.
2. `subset()` takes subsets of row in a data frame.
Most useful in interactive programming!
The `$` is similar to `[[`:
```{r}
x[["a"]]
x$"a"
```
Use quotes with `$` when names contain characters that are R operators (such as
`+`, `-`, `>`, ...):
```{r}
x$"+A"
```
Unlike `[[`, the `$` will try to match partial names:
```{r}
dogs$bree # breed
```
The `subset()` function is a shortcut to avoid typing the name of the data
frame over and over when subsetting rows with a condition.
For example:
```{r}
head(dogs)
colnames(dogs)
above = dogs$weight > mean(dogs$weight, na.rm = TRUE)
# which() to eliminate NAs
dogs[which(above), ]
```
Equivalent, using `subset()`:
```{r}
subset(dogs, weight > mean(weight, na.rm = TRUE))
```
With `subset()`, there's no need to use `$`, and the `NA`s are eliminated
automatically.
Transforming Data Frames
------------------------
A few more functions for working with data frames:
* `rbind()` stacks two or more data frames on top of each other.
* `cbind()` combines two or more data frames side-by-side.
* `t()` transposes a data frame (or matrix).
Examples:
```{r}
dogs[1:3, ]
rbind(dogs[1:3, ], dogs[1:3, ])
```
These functions also work with matrices.
Creating Visualizations
=======================
There are three main systems for creating visualizations in R:
1. The base R functions, including `plot()`.
2. The `lattice` package. The interface is similar to the base R
functions, but uses lists of parameters to control plot details.
3. The `ggplot2` package. The interface is a "grammar of graphics"
where plots are assembled from layers.
Both `lattice` and `ggplot2` are based on R's low-level `grid`
graphics system.
It is usually easier to customize visualizations made with base R.
Both `lattice` and `ggplot2` are better at handling grouped data and
generally require less code to create a nice-looking visualization.
We'll learn `ggplot2`.
R Packages and CRAN
-------------------
The Comprehensive R Archive Network (CRAN) is a repository of
user-contributed packages for R.
You can install packages from CRAN with `install.packages()`.
For example:
```{r}
install.packages("ggplot2")
```
For maintaining your packages, there are also functions:
* `remove.packages()` to remove a package
* `update.packages()` to update ALL packages
* `installed.packages()` to list installed packages
You can load an installed package into your R session with the
`library()` function.
```{r}
library(ggplot2)
```
Install once, load with `library()` each time you restart R.
The Tidyverse
-------------
The Tidyverse (<https://www.tidyverse.org/>) is collection of R
packages for doing data science in R.
They are made by many of the same people that make RStudio, and
provide alternatives to R's built-in tools for:
* Reading files (package `readr`)
* Manipulating data frames (packages `dplyr`, `tidyr`, `tibble`)
* Manipulating strings (package `stringr`)
* Manipulating factors (package `forcats`)
* Functional programming (package `purrr`)
* Making visualizations (package `ggplot2`)
The Tidyverse packages are popular but controversial, because some of
them use a syntax different from base R.
RStudio cheat sheets (mostly for Tidyverse packages):
https://rstudio.com/resources/cheatsheets/
Making Graphics with ggplot2
----------------------------
The fundamental idea of `ggplot2` is that all graphics are composed
of layers.
Documentation:
https://ggplot2.tidyverse.org/
As an example, let's create a simplifed version of the Best in Show
visualization:
https://informationisbeautiful.net/visualizations/
best-in-show-whats-the-top-data-dog/
Layer 1: Data -- the data frame to plot.
Call the `ggplot()` function to set the data layer:
```{r}
library(ggplot2)
ggplot(dogs)
```
Layer 2: GEOMetry -- shapes to represent data.
Add a `geom_` function to set the geometry layer:
```{r}
ggplot(dogs) + geom_point()
```
Layer 3: AESthetic -- "wires" between geometry and data
Call the `aes()` function in `ggplot()` or the `geom_` to set the
mapping between the data layer and geometry layer:
```{r}
ggplot(dogs, aes(x = datadog, y = popularity)) + geom_point()
# Alternative syntax:
ggplot(dogs) + geom_point(aes(x = datadog, y = popularity))
```
Important layers in `ggplot2`:
Layer | Description
---------- | -----------
data | A data frame to visualize
aesthetics | The map or "wires" between data and geometry
geometry | Geometry to represent the data visually
statistics | An alternative to geometry
scales | How numbers in data are converted to numbers on screen
labels | Titles and axis labels
guides | Legend settings
annotations | Additional geoms that are not mapped to data
coordinates | Coordinate systems (Cartesian, logarithmic, polar)
facets | Side-by-side panels
Saving Your Plots
-----------------
Use `ggsave()` to save the last plot you created to a file:
```{r}
ggsave("myplot.png")
```