forked from batpigandme/tidynomicon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebt.Rmd
473 lines (371 loc) · 14.2 KB
/
debt.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
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
# Intellectual Debt {#debt}
```{r setup, include=FALSE}
source("etc/common.R")
```
We have accumulated some intellectual debt in the previous lessons,
and we should clear this burden from our conscience before we go on to new topics.
## Learning Objectives
- Explain what the formula operator `~` was created for and what other uses it has.
- Describe and use `.`, `.x`, `.y, `..1`, `..2`, and other convenience parameters.
- Define copy-on-modify and explain its use in R.
## Why shouldn't I use `setwd`?
Because [reasons][bryan-setwd].
**But…**
No.
Use the [here package][here-package] instead to create paths that are relative to your current location:
```{r how-here-works}
print(glue('here by itself: {here()}'))
print(glue('here("book.bib"): {here("book.bib")}'))
print(glue('here("etc", "common.R"): {here("etc", "common.R")}'))
```
## What the hell are factors?
Another feature of R that doesn't have an exact analog in Python is [factors](glossary.html#factor).
In statistics, a factor is a categorical variable such as "flavor",
which can be "vanilla", "chocolate", "strawberry", or "mustard".
Factors can be represented as strings,
but storing the same string many times wastes space and is inefficient
(since comparing strings takes longer than comparing numbers).
R therefore stores each string once and gives it with a numeric key,
so that internally, "mustard" is the number 4 in the lookup table for "flavor",
but is presented as "mustard" rather than 4.
This is useful, but brings with it some problems:
1. On the statistical side,
it encourages people to put messy reality into tidy but misleading boxes.
For example, it's unfortunately still common for forms to require people to identify themselves
as either "male" or "female",
which is [scientifically](https://www.quora.com/Scientifically-how-many-sexes-genders-are-there)
[incorrect](https://www.joshuakennon.com/the-six-common-biological-sexes-in-humans/).
Similarly, census forms that ask questions about racial or ethnic identity often leave people scratching their heads,
since they don't belong to any of the categories offered.
2. On the computational side,
some functions in R automatically convert strings to factors by default.
This makes sense when working with statistical data—in most cases,
a column in which the same strings are repeated many times is categorical—but
it is usually not the right choice in other situations.
This has surprised enough people the years that the tidyverse goes the other way
and only creates factors when asked to.
Let's work through a small example.
Suppose we've read a CSV file and wound up with this table:
```{r person-flavor-ranking}
raw <- tribble(
~person, ~flavor, ~ranking,
"Lhawang", "strawberry", 1.7,
"Lhawang", "chocolate", 2.5,
"Lhawang", "mustard", 0.2,
"Khadee", "strawberry", 2.1,
"Khadee", "chocolate", 2.4,
"Khadee", "vanilla", 3.9,
"Haddad", "strawberry", 1.8,
"Haddad", "vanilla", 2.1
)
raw
```
Let's aggregate using flavor values so that we can check our factor-based aggregating later:
```{r aggregate-flavor-values}
raw %>%
group_by(flavor) %>%
summarize(number = n(), average = mean(ranking))
```
It probably doesn't make sense to turn `person` into factors,
since names are actually character strings,
but `flavor` is a good candidate:
```{r convert-flavor-to-factor}
raw <- mutate_at(raw, vars(flavor), as.factor)
raw
```
We can still aggregate as we did before:
```{r aggregate-with-factor}
raw %>%
group_by(flavor) %>%
summarize(number = n(), average = mean(ranking))
```
We can also impose an ordering on the factor's elements:
```{r order-with-factor}
raw <- raw %>%
mutate(flavor = fct_relevel(flavor, "chocolate", "strawberry", "vanilla", "mustard"))
raw
```
This changes the order in which they are displayed after grouping:
```{r order-after-grouping}
raw %>%
group_by(flavor) %>%
summarize(number = n(), average = mean(ranking))
```
And also changes the order of bars in a bar chart:
```{r simple_bar_chart}
raw %>%
group_by(flavor) %>%
summarize(number = n(), average = mean(ranking)) %>%
ggplot(mapping = aes(x = flavor, y = average)) +
geom_col()
```
To learn more about how factors work and how to use them when analyzing categorical data,
please see [this paper](https://peerj.com/preprints/3163/) by McNamara and Horton.
## How do I refer to various arguments in a pipeline?
When we put a function in a pipeline using `%>%`,
that operator calls the function with the incoming data as the first argument,
so `data %>% func(arg)` is the same as `func(data, arg)`.
This is fine when we want the incoming data to be the first argument,
but what if we want it to be second? Or third?
One possibility is to save the result so far in a temporary variable
and then start a second pipe:
```{r create-temps}
data <- tribble(
~left, ~right,
1, NA,
2, 20
)
empties <- data %>%
pmap_lgl(function(...) {
args <- list(...)
any(is.na(args))
})
data %>%
transmute(id = row_number()) %>%
filter(empties) %>%
pull(id)
```
This builds a logical vector `empties` with as many entries as `data` has rows,
then filters data according to which of the entries in the vector are `TRUE`.
A better practice is to use the parameter name `.`,
which means "the incoming data".
In some functions (e.g., a two-argument function being used in `map`)
we can also use `.x` and `.y` for the first and second arguments,
and for more arguments,
we can use `..1`, `..2`, and so on (with two dots at the front):
```{r using-dot}
data %>%
pmap_lgl(function(...) {
args <- list(...)
any(is.na(args))
}) %>%
tibble(empty = .) %>%
mutate(id = row_number()) %>%
filter(empty) %>%
pull(id)
```
In this model,
we create the logical vector,
then turn it into a tibble with one column called `empty`
(which is what `empty = .` does in `tibble`'s constructor).
After that,
we add another column with row numbers,
filter,
and pull out the row numbers.
And while we're here:
`row_number` doesn't do what its name suggests.
We're better off using `rowid_to_column`:
```{r rowid-to-column}
data %>%
rowid_to_column()
```
## I thought you said that R encouraged functional programming?
I did.
Here is a function that reads a file and returns one of its columns:
```{r define-function, message=FALSE}
col_from_file <- function(filename, colname) {
dat <- readr::read_csv(filename)
dat[colname]
}
person_filename <- here::here("data", "person.csv")
col_from_file(person_filename, "family_name")
```
Note that the column name *must* be passed as a quoted string;
Chapter \@ref(nse) will show us how to pass unquoted column names.
We might occasionally want to allow the user to specify
what values in the file are to be considered NAs.
This small addition allows us to do that,
while keeping the empty string and the string `"NA"` as defaults:
```{r default-value, message=FALSE}
col_from_file <- function(filename, colname, na = c("", "NA")) {
dat <- readr::read_csv(filename, na = na)
dat[colname]
}
col_from_file(person_filename, "family_name", c("Dyer"))
```
We can also allow the user to specify any number of columns
by capturing "extra" parameters in `...`
and passing that value directly to `dplyr::select`:
```{r quote-multi-column, message=FALSE}
cols_from_file <- function(filename, ..., na = c("", "NA")) {
readr::read_csv(filename, na = na) %>%
dplyr::select(...)
}
cols_from_file(person_filename, personal_name, family_name)
```
Now that we can create functions,
we can use the tools in the `purrr` library to wield them.
`purrr::map` applies a function to each value in a vector in turn
and returns a list:
```{r purrr-map}
is_long_name <- function(name) {
stringr::str_length(name) > 4
}
person <- read_csv(here::here("data", "person.csv"))
purrr::map(person$family_name, is_long_name)
```
For small calculations,
we will define the function where it is used—this is sometimes called
an [anonymous function](glossary.html#anonymous-function)
since it isn't given a name.
We will also use `purrr::map_lgl`
so that the result of the call is a logical vector rather than a list.
Similarly-named functions will give us numbers, character strings, and so on:
```{r anonymous-function}
purrr::map_lgl(person$family_name,
function(name) stringr::str_length(name) > 4)
```
Little functions like this are so common
that `purrr` allows us to use write them as formulas using the `~ operator
with `.x` as a shorthand for the value from the vector being processed:
```{r}
purrr::map_chr(person$family_name, ~ stringr::str_to_upper(.x))
```
Other functions in `purrr` let us work on two vectors at once:
```{r}
purrr::map2_chr(person$personal_name,
person$family_name,
~ stringr::str_c(.y, .x, sep = '_'))
```
If we need to collapse the result to a single value
(e.g., to use in `if`)
we have `purrr::some` and `purrr::every`:
```{r}
purrr::every(person$personal_name, ~ .x > 'M')
```
### Modify specific elements of a list:
```{r}
purrr::modify_at(person$personal_name, c(2, 4), stringr::str_to_upper)
```
*Use `modify_if` to upper-case names that are greater than "M".*
### Create an acronym:
```{r}
purrr::reduce(person$personal_name, ~stringr::str_c(.x, stringr::str_sub(.y, 1, 1)), .init = "")
```
*Explain why using `stringr::str_c(stringr::str_sub(.x, 1, 1), stringr::str_sub(.y, 1, 1))` doesn't work.*
### Create intermediate values:
```{r}
purrr::accumulate(person$personal_name, ~stringr::str_c(.x, stringr::str_sub(.y, 1, 1)), .init = "")
```
*Modify this so that the initial empty string isn't in the final result.*
## How does R give the appearance of immutable data?
Another feature of R that can surprise the unwary is its use of [copy-on-modify](glossary.html#copy-on-modify)
to make data appear [immutable](glossary.html#immutable)
(a jargon term meaning "cannot be changed after creation").
If two or more variables refer to the same data
and that data is updated via one variable,
R automatically makes a copy of the data so that the other variable's value doesn't change.
Here's a simple example:
```{r immutable-vec}
first <- c("red", "green", "blue")
second <- first
print(glue("before modification, first is {paste(first, collapse='-')} and second is {paste(second, collapse='-')}"))
first[[1]] <- "sulphurous"
print(glue("after modification, first is {paste(first, collapse='-')} and second is {paste(second, collapse='-')}"))
```
This is true of nested structures as well:
```{r immutable-tibble}
first <- tribble(
~left, ~right,
101, 202,
303, 404)
second <- first
first$left[[1]] <- 999
print("first after modification")
first
print("second after modification")
second
```
In this case,
the entire `left` column of `first` has been replaced:
tibbles (and data frames) are stored as lists of vectors,
so changing any value in a column triggers construction of a new column vector.
We can watch this happen using the `tracemem` function,
which shows us where objects live in the computer's memory:
```{r pryr}
first <- tribble(
~left, ~right,
101, 202,
303, 404
)
tracemem(first)
first$left[[1]] <- 999
untracemem(first)
```
This rather cryptic output tell us the address of the tibble,
then notifies us of changes to the tibble and its contents.
We can accomplish something a little more readable using `pryr::address`
(i.e., the `address` function from the pryr package):
```{r address}
left <- first$left # alias
print(glue("left column is initially at {pryr::address(left)}"))
first$left[[2]] <- 888
print(glue("after modification, the original column is still at {pryr::address(left)}"))
temp <- first$left # another alias
print(glue("but the first column is at {pryr::address(temp)}"))
```
(We need to use the [alias](glossary.html#alias) `temp` because `address(first$left)` doesn't work:
the argument to `address` needs to be a variable name.)
R's copy-on-modify semantics is particularly important when writing functions.
If we modify an argument inside a function,
that modification isn't visible to the caller,
so even functions that appear to modify structures usually don't.
("Usually", because there are exceptions, but we must stray off the path to find them.)
## What else should I worry about?
Ralph Waldo Emerson once wrote, "A foolish consistency is the hobgoblin of little minds."
Here, then, are few of the hobgoblins I've encountered on my journey through R.
### The `order` function
The function `order` generates indices to pull values into place rather than push them,
i.e.,
`order(x)[i]` is the index in `x` of the element that belongs at location `i`.
For example:
```{r order-func}
bases <- c("g", "c", "t", "a")
order(bases)
```
shows that the value at location 4 (the `"a"`) belongs in the first spot of the vector;
it does *not* mean that the value in the first location (the `"g"`) belongs in location 4.
This convention means that `something[order(something)]` does the right thing:
```{r}
bases[order(bases)]
```
### One of a set of values
The function `one_of` is a handy way to specify several values for matching
without complicated Boolean conditionals.
For example,
`gather(data, key = "year", value = "cases", one_of(c("1999", "2000")))`
collects data for the years 1999 and 2000.
### `|` and `&` are not the same as `||` and `&&`
Let's try some experiments:
```{r}
TRUE_TRUE <- c(TRUE, TRUE)
TRUE_FALSE <- c(TRUE, FALSE)
FALSE_TRUE <- c(FALSE, TRUE)
print(glue("TRUE_TRUE & TRUE_FALSE: {paste(TRUE_TRUE & TRUE_FALSE, collapse = ' ')}"))
print(glue("TRUE_TRUE & FALSE_TRUE: {paste(TRUE_TRUE & FALSE_TRUE, collapse = ' ')}"))
print(glue("TRUE_TRUE && TRUE_FALSE: {paste(TRUE_TRUE && TRUE_FALSE, collapse = ' ')}"))
print(glue("TRUE_TRUE && FALSE_TRUE: {paste(TRUE_TRUE && FALSE_TRUE, collapse = ' ')}"))
```
The difference is that `&` always returns a vector result after doing element-by-element conjunction,
while `&&` returns a scalar result.
This means that `&` is almost always what we want to use when working with data.
### Functions and columns
There is a function called `n`.
It's not the same thing as a column called `n`.
I only made this mistake a dozen times.
```{r func-col-n}
data <- tribble(
~a, ~n,
1, 10,
2, 20
)
data %>% summarize(total = sum(n))
```
```{r}
data %>% summarize(total = sum(n()))
```
## Key Points
```{r keypoints, child="keypoints/debt.md"}
```
```{r links, child="etc/links.md"}
```