forked from batpigandme/tidynomicon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtidyverse.Rmd
846 lines (666 loc) · 25.9 KB
/
tidyverse.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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# The Tidyverse {#tidyverse}
```{r setup, include=FALSE}
source("etc/common.R")
```
There is no point in becoming fluent in Enochian if you do not then call forth a Dweller Beneath at the time of the new moon.
Similarly,
there is no point learning a language designed for data manipulation if you do not then bend data to your will.
This chapter therefore looks at how to do the things that R was summoned—er, designed—to do.
## Learning Objectives
- Install and load packages in R.
- Read CSV data with R.
- Explain what a tibble is and how tibbles related to data frames and matrices.
- Describe how `read_csv` infers data types for columns in tabular datasets.
- Name and use three functions for inspects tibbles.
- Select subsets of tabular data using column names, scalar indices, ranges, and logical expressions.
- Explain the difference between indexing with `[` and with `[[`.
- Name and use four functions for calculating aggregate statistics on tabular data.
- Explain how these functions treat `NA` by default, and how to change that behavior.
## How do I read data?
We begin by looking at the file `results/infant_hiv.csv`,
a tidied version of data on the percentage of infants born to women with HIV
who received an HIV test themselves within two months of birth.
The original data comes from the UNICEF site at <https://data.unicef.org/resources/dataset/hiv-aids-statistical-tables/>,
and this file contains:
```
country,year,estimate,hi,lo
AFG,2009,NA,NA,NA
AFG,2010,NA,NA,NA
...
AFG,2017,NA,NA,NA
AGO,2009,NA,NA,NA
AGO,2010,0.03,0.04,0.02
AGO,2011,0.05,0.07,0.04
AGO,2012,0.06,0.08,0.05
...
ZWE,2016,0.71,0.88,0.62
ZWE,2017,0.65,0.81,0.57
```
The actual file has many more rows (and no ellipses).
It uses `NA` to show missing data rather than (for example) `-`, a space, or a blank,
and its values are interpreted as follows:
| Header | Datatype | Description |
|----------|-----------|---------------------------------------------|
| country | char | ISO3 country code of country reporting data |
| year | integer | year CE for which data reported |
| estimate | double/NA | estimated percentage of measurement |
| hi | double/NA | high end of range |
| lo | double/NA | low end of range |
We can load this data in Python like this:
```{python python-read-csv, output.lines=10}
import pandas as pd
infant_hiv = pd.read_csv('results/infant_hiv.csv')
print(infant_hiv)
```
The equivalent in R is to load
the [tidyverse](glossary.html#tidyverse) collection of [packages](glossary.html#package)
and then call the `read_csv` function.
We will go through this in stages, since each produces output.
```{r library-fail, eval=FALSE}
library(tidyverse)
```
```
Error in library(tidyverse) : there is no package called 'tidyverse'
```
Ah.
We must install the tidyverse
(but only need to do so once per machine):
```{r library-install, eval=FALSE}
install.packages("tidyverse")
```
At any time,
we can call `sessionInfo` to find out what versions of which packages we have loaded,
along with the version of R we're using and some other useful information:
```{r session-info}
sessionInfo()
```
We then load the library once per program:
```{r unloading, echo=FALSE}
# Detach so that loading will be echoed.
detach(package:tidyverse)
```
```{r library-succeed}
library(tidyverse)
```
Note that we give `install.packages` a string to install,
but simply give the name of the package we want to load to `library`.
Loading the tidyverse gives us eight packages.
One of those, dplyr, defines two functions that mask standard functions in R with the same names.
If we need the originals,
we can always get them with their
[fully-qualified names](glossary.html#fully-qualified-name)
`stats::filter` and `stats::lag`.
Once we have the tidyverse loaded,
reading the file looks remarkably like reading the file:
```{r r-read-csv}
infant_hiv <- read_csv('results/infant_hiv.csv')
```
R's `read_csv` tells us more about what it has done than Pandas does.
In particular, it guesses the data types of columns based on the first thousand values
and then tells us what types it has inferred.
(In a better universe,
people would habitually use the first *two* rows of their spreadsheets for name *and units*,
but we do not live there.)
We can now look at what `read_csv` has produced.
```{r show-tibble}
infant_hiv
```
This is a [tibble](glossary.html#tibble),
which is the tidyverse's enhanced version of R's `data.frame`.
It organizes data into named columns,
each having one value for each row.
In statistical terms,
the columns are the variables being observed
and the rows are the actual observations.
## How do I inspect data?
We often have a quick look at the content of a table to remind ourselves what it contains.
Pandas does this using methods whose names are borrowed from the Unix shell's `head` and `tail` commands:
```{python py-show-head}
print(infant_hiv.head())
```
```{python py-show-tail}
print(infant_hiv.tail())
```
R has similarly-named functions:
```{r r-show-head}
head(infant_hiv)
```
```{r r-show-tail}
tail(infant_hiv)
```
Let's have a closer look at that last command's output:
```{r r-tail-tibble, paged.print=FALSE}
tail(infant_hiv)
```
Note that the row numbers printed by `tail` are [relative](glossary.html#relative-row-number) to the output,
not [absolute](glossary.html#absolute-row-number) to the table.
This is different from Pandas,
which retains the original row numbers.
What about overall information?
```{python data-info}
print(infant_hiv.info())
```
```{r data-summary}
summary(infant_hiv)
```
Your display of R's summary may or may not wrap,
depending on how large a screen the older acolytes have allowed you.
## How do I index rows and columns?
A Pandas DataFrame is a collection of series (also called columns),
each containing the values of a single observed variable:
```{python py-string-subscript, output.lines=NA}
print(infant_hiv['estimate'])
```
We would get exactly the same output in Python with `infant_hiv.estimate`,
i.e.,
with an attribute name rather than a string subscript.
The same tricks work in R:
```{r r-string-subscript}
infant_hiv['estimate']
```
However, R's `infant_hiv$estimate` provides all the data:
```{r r-dollar-subscript, output.lines=NA}
infant_hiv$estimate
```
Again, note that the boxed number on the left is the start index of that row.
What about single values?
Remembering to count from zero from Python and as humans do for R,
we have:
```{python py-individual-element}
print(infant_hiv.estimate[11])
```
```{r r-individual-element}
infant_hiv$estimate[12]
```
Ah—everything in R is a vector,
so we get a vector of one value as an output rather than a single value.
```{python py-len-individual-element, error=TRUE}
print(len(infant_hiv.estimate[11]))
```
```{r r-len-individual-element}
length(infant_hiv$estimate[12])
```
And yes, ranges work:
```{python py-range-estimate}
print(infant_hiv.estimate[5:15])
```
```{r r-range-estimate}
infant_hiv$estimate[6:15]
```
Note that the upper bound is the same, because it's inclusive in R and exclusive in Python.
Note also that nothing prevents us from selecting a range of rows that spans several countries,
which is why selecting by row number is usually a sign of innocence, insouciance, or desperation.
We can select by column number as well.
Pandas uses the rather clumsy `object.iloc[rows, columns]` with the usual shortcut `:` for "entire range":
```{python py-iloc, output.lines=NA}
print(infant_hiv.iloc[:, 0])
```
Since this is a column, it can be indexed:
```{python py-iloc-indexed}
print(infant_hiv.iloc[:, 0][0])
```
In R, a single index is interpreted as the column index:
```{r single-index-is-col}
infant_hiv[1]
```
But notice that the output is not a vector, but another tibble (i.e., a table with N rows and one column).
This means that adding another index does column-wise indexing on that tibble:
```{r double-index-of-tibble}
infant_hiv[1][1]
```
How then are we to get the first mention of Afghanistan?
The answer is to use [double square brackets](glossary.html#double-square-brackets) to strip away one level of structure:
```{r double-square-on-tibble, output.lines=NA}
infant_hiv[[1]]
```
This is now a plain old vector, so it can be indexed with [single square brackets](glossary.html#single-square-brackets):
```{r double-square-then-single}
infant_hiv[[1]][1]
```
But that too is a vector, so it can of course be indexed as well (for some value of "of course"):
```{r double-single-single}
infant_hiv[[1]][1][1]
```
Thus,
`data[1][[1]]` produces a tibble,
then selects the first column vector from it,
so it still gives us a vector.
*This is not madness.*
It is merely…differently sane.
> **Subsetting Data Frames**
>
> When we are working with data frames (including tibbles),
> subsetting with a single vector selects columns, not rows,
> because data frames are stored as lists of columns.
> This means that `df[1:2]` selects two columns from `df`.
> However, in `df[2:3, 1:2]`, the first index selects rows, while the second selects columns.
## How do I calculate basic statistics?
What is the average estimate?
We start by grabbing that column for convenience:
```{python py-average-index}
estimates = infant_hiv.estimate
print(len(estimates))
```
```{python py-estimates-mean}
print(estimates.mean())
```
This translates almost directly to R:
```{r r-average-index}
estimates <- infant_hiv$estimate
length(estimates)
```
```{r r-estimates-mean}
mean(estimates)
```
The void is always there, waiting for us…
Let's fix this in R first by telling `mean` to drop NAs:
```{r r-remove-na}
mean(estimates, na.rm = TRUE)
```
and then try to get the statistically correct behavior in Pandas:
```{python py-remove-na}
print(estimates.mean(skipna=False))
```
Many functions in R use `na.rm` to control whether `NA`s are removed or not.
(Remember, the `.` character is just another part of the name)
R's default behavior is to leave `NA`s in, and then to include them in [aggregate](glossary.html#aggregation) computations.
Python's is to get rid of missing values early and work with what's left,
which makes translating code from one language to the next much more interesting than it might otherwise be.
But other than that, the statistics works the same way.
In Python, we write:
```{python py-min-max-std}
print("min", estimates.min())
print("max", estimates.max())
print("std", estimates.std())
```
and in R:
```{r r-min-max-std}
print(glue("min {min(estimates, na.rm = TRUE)}"))
print(glue("max {max(estimates, na.rm = TRUE)}"))
print(glue("sd {sd(estimates, na.rm = TRUE)}"))
```
A good use of aggregation is to check the quality of the data.
For example,
we can ask if there are any records where some of the estimate, the low value, or the high value are missing,
but not all of them:
```{python py-check-null}
print((infant_hiv.hi.isnull() != infant_hiv.lo.isnull()).any())
```
```{r r-check-null}
any(is.na(infant_hiv$hi) != is.na(infant_hiv$lo))
```
## How do I filter data?
By "[filtering](glossary.html#filter)", we mean "selecting records by value".
As discussed in Chapter \@ref(basics),
the simplest approach is to use a vector of logical values to keep only the values corresponding to `TRUE`.
In Python, this is:
```{python py-simple-filter}
maximal = estimates[estimates >= 0.95]
print(len(maximal))
```
And in R:
```{r r-simple-filter}
maximal <- estimates[estimates >= 0.95]
length(maximal)
```
The difference is unexpected.
Let's have a closer look at the result in Python:
```{python py-maximal, output.lines=NA}
print(maximal)
```
And in R:
```{r r-maximal, output.lines=NA}
maximal
```
It appears that R has kept the unknown values in order to highlight just how little we know.
More precisely,
wherever there was an `NA` in the original data
there is an `NA` in the logical vector
and hence an `NA` in the final vector.
Let us then turn to `which` to get a vector of indices at which a vector contains `TRUE`.
This function does not return indices for `FALSE` or `NA`:
```{r r-which}
which(estimates >= 0.95)
```
And as a quick check:
```{r r-length-which}
length(which(estimates >= 0.95))
```
So now we can index our vector with the result of the `which`:
```{r r-maximal-which}
maximal <- estimates[which(estimates >= 0.95)]
maximal
```
But should we do this?
Those `NA`s are important information,
and should not be discarded so blithely.
What we should *really* be doing is using the tools the tidyverse provides
rather than clever indexing tricks.
These behave consistently across a wide scale of problems
and encourage use of patterns that make it easier for others to understand our programs.
## How do I write tidy code?
The six basic data transformation operations in the tidyverse are:
- `filter`: choose observations (rows) by value(s)
- `arrange`: reorder rows
- `select`: choose variables (columns) by name
- `mutate`: derive new variables from existing ones
- `group_by`: define subsets of rows for further processing
- `summarize`: combine many values to create a single new value
`filter(tibble, ...criteria...)` keeps rows that pass all of the specified criteria:
```{r filter-as-function}
filter(infant_hiv, lo > 0.5)
```
Notice that the expression is `lo > 0.5` rather than `"lo" > 0.5`.
The latter expression would return the entire table
because the string `"lo"` is greater than the number 0.5 everywhere.
But how is it that the name `lo` can be used on its own?
It is the name of a column, but there is no variable called `lo`.
The answer is that R uses [lazy evaluation](glossary.html#lazy-evaluation):
function arguments aren't evaluated until they're needed,
so the function `filter` actually gets the expression `lo > 0.5`,
which allows it to check that there's a column called `lo` and then use it appropriately.
It may seem strange at first,
but it is much tidier than `filter(data, data$lo > 0.5)` or `filter(data, "lo > 0.5")`.
We will explore lazy evaluation further in Chapter \@ref(nse).
We can make data anlaysis code more readable by using the [pipe operator](glossary.html#pipe-operator) `%>%`:
```{r filter-in-pipe}
infant_hiv %>% filter(lo > 0.5)
```
This may not seem like much of an improvement,
but neither does a Unix pipe consisting of `cat filename.txt | head`.
What about this?
```{r filter-complex}
filter(infant_hiv, (estimate != 0.95) & (lo > 0.5) & (hi <= (lo + 0.1)))
```
It uses the vectorized "and" operator `&` twice,
and parsing the condition takes a human being at least a few seconds.
Its pipelined equivalent is:
```{r filter-complex-pipe}
infant_hiv %>% filter(estimate != 0.95) %>% filter(lo > 0.5) %>% filter(hi <= (lo + 0.1))
```
Breaking the condition into stages like this often makes reading and testing much easier,
and encourages incremental write-test-extend development.
Let's increase the band from 10% to 20%,
break the line the way the [tidyverse style guide][tidyverse-style] recommends
to make the operations easier to spot,
and order by `lo` in descending order:
```{r filter-arrange}
infant_hiv %>%
filter(estimate != 0.95) %>%
filter(lo > 0.5) %>%
filter(hi <= (lo + 0.2)) %>%
arrange(desc(lo))
```
We can now [select](glossary.html#select) the three columns we care about:
```{r filter-arrange-select}
infant_hiv %>%
filter(estimate != 0.95) %>%
filter(lo > 0.5) %>%
filter(hi <= (lo + 0.2)) %>%
arrange(desc(lo)) %>%
select(year, lo, hi)
```
Once again,
we are using the unquoted column names `year`, `lo`, and `hi`
and letting R's lazy evaluation take care of the details for us.
Rather than selecting these three columns,
we can [select *out*](glossary.html#negative-selection) the columns we're not interested in
by negating their names.
This leaves the columns that are kept in their original order,
rather than putting `lo` before `hi`,
which won't matter if we later select by name,
but *will* if we ever want to select by position:
```{r select-out}
infant_hiv %>%
filter(estimate != 0.95) %>%
filter(lo > 0.5) %>%
filter(hi <= (lo + 0.2)) %>%
arrange(desc(lo)) %>%
select(-country, -estimate)
```
Giddy with power,
we now add a column containing the difference between the low and high values.
This can be done using either `mutate`,
which adds new columns to the end of an existing tibble,
or with `transmute`,
which creates a new tibble containing only the columns we explicitly ask for.
(There is also a function `rename` which simply renames columns.)
Since we want to keep `hi` and `lo`,
we decide to use `mutate`:
```{r mutate-new-column}
infant_hiv %>%
filter(estimate != 0.95) %>%
filter(lo > 0.5) %>%
filter(hi <= (lo + 0.2)) %>%
arrange(desc(lo)) %>%
select(-country, -estimate) %>%
mutate(difference = hi - lo)
```
Does the difference between high and low estimates vary by year?
To answer that question,
we use `group_by` to [group](glossary.html#group) records by value
and then `summarize` to aggregate within groups.
We might as well get rid of the `arrange` and `select` calls in our pipeline at this point,
since we're not using them,
and count how many records contributed to each aggregation using `n()`:
```{r summarize-and-count}
infant_hiv %>%
filter(estimate != 0.95) %>%
filter(lo > 0.5) %>%
filter(hi <= (lo + 0.2)) %>%
mutate(difference = hi - lo) %>%
group_by(year) %>%
summarize(count = n(), ave_diff = mean(year))
```
How might we do this with Pandas?
One approach is to use a single multi-part `.query` to select data
and store the result in a variable so that we can refer to the `hi` and `lo` columns twice
without repeating the filtering expression.
We then group by year and aggregate, again using strings for column names:
```{python equivalent-to-pipeline}
data = pd.read_csv('results/infant_hiv.csv')
data = data.query('(estimate != 0.95) & (lo > 0.5) & (hi <= (lo + 0.2))')
data = data.assign(difference = (data.hi - data.lo))
grouped = data.groupby('year').agg({'difference' : {'ave_diff' : 'mean', 'count' : 'count'}})
print(grouped)
```
There are other ways to tackle this problem with Pandas,
but the tidyverse approach produces code that I find more readable.
## How do I model my data?
Tidying up data can be as calming and rewarding in the same way as knitting
or rearranging the specimen jars on the shelves in your dining room-stroke-laboratory.
Eventually,
though,
people want to do some statistics.
The simplest tool for this in R is `lm`, which stands for "linear model".
Given a formula and a data set,
it calculates coefficients to fit that formula to that data:
```{r simple-formula}
lm(estimate ~ lo, data = infant_hiv)
```
This is telling us that `estimate` is more-or-less equal to `0.0421 + 1.0707 * lo`.
The `~` symbol is used to separate the left and right sides of the equation,
and as with all things tidyverse,
lazy evaluation allows us to use variable names directly.
In fact,
it lets us write much more complex formulas involving functions of multiple variables.
For example,
we can regress `estimate` against the square roots of `lo` and `hi`
(though there is no sound statistical reason to do so):
```{r complex-formula}
lm(estimate ~ sqrt(lo) + sqrt(hi), data = infant_hiv)
```
One important thing to note here is the way that `+` is overloaded in formulas.
The formula `estimate ~ lo + hi` does *not* mean "regress `estimate` against the sum of `lo` and `hi`",
but rather, "regress `estimate` against the two variables `lo` and `hi`":
```{r double-regression}
lm(estimate ~ lo + hi, data = infant_hiv)
```
If we want to regress `estimate` against the average of `lo` and `hi`
(i.e., regress `estimate` against a single calculated variable instead of against two variables)
we need to create a temporary column:
```{r regress-temporary}
infant_hiv %>%
mutate(ave_lo_hi = (lo + hi)/2) %>%
lm(estimate ~ ave_lo_hi, data = .)
```
Here, the call to `lm` is using the variable `.` to mean
"the data coming in from the previous stage of the pipeline".
Most of the functions in the tidyverse use this convention
so that data can be passed to a function that expects it in a position other than the first.
## How do I create a plot?
Human being always want to see the previously unseen,
though they are not always glad to have done so.
The most popular tool for doing this in R is `ggplot2`,
which implements and extends the patterns for creating charts described in @Wilk2005.
Every chart it creates has a [geometry](glossary.html#geometry) that controls how data is displayed
and a [mapping](glossary.html#mapping) that controls how values are represented geometrically.
For example,
these lines of code create a scatter plot
showing the relationship between `lo` and `hi` values in the infant HIV data:
```{r basic-plot}
ggplot(infant_hiv) + geom_point(mapping = aes(x = lo, y = hi))
```
Looking more closely:
- The function `ggplot` creates an object to represent the chart with `infant_hiv` as the underlying data.
- `geom_point` specifies the geometry we want (points).
- Its `mapping` argument is assigned an [aesthetic](glossary.html#aesthetic)
that specifies `lo` is to be used as the `x` coordinate and `hi` is to be used as the `y` coordinate.
- The elements of the chart are combined with `+` rather than `%>%` for historical reasons.
Let's create a slightly more appealing plot by dropping NAs,
making the points semi-transparent,
and colorizing them according to the value of `estimate`:
```{r plot-after-drop}
infant_hiv %>%
drop_na() %>%
ggplot(mapping = aes(x = lo, y = hi, color = estimate)) +
geom_point(alpha = 0.5) +
xlim(0.0, 1.0) + ylim(0.0, 1.0)
```
We set the transparency `alpha` outside the aesthetic because its value is constant for all points.
If we set it inside `aes(...)`,
we would be telling ggplot2 to set the transparency according to the value of the data.
We specify the limits to the axes manually with `xlim` and `ylim` to ensure that ggplot2 includes the upper bounds:
without this,
all of the data would be shown,
but the upper label "1.00" would be omitted.
This plot immediately shows us that we have some outliers.
There are far more values with `hi` equal to 0.95 than it seems there ought to be,
and there are eight points running up the left margin that seem troubling as well.
Let's create a new tibble that doesn't have these:
```{r plot-remove-outliers}
infant_hiv %>%
drop_na() %>%
filter(hi != 0.95) %>%
filter(!((lo < 0.10) & (hi > 0.25))) %>%
ggplot(mapping = aes(x = lo, y = hi, color = estimate)) +
geom_point(alpha = 0.5) +
xlim(0.0, 1.0) + ylim(0.0, 1.0)
```
We can add the fitted curve by including another geometry called `geom_smooth`:
```{r plot-with-fit}
infant_hiv %>%
drop_na() %>%
filter(hi != 0.95) %>%
filter(!((lo < 0.10) & (hi > 0.25))) %>%
ggplot(mapping = aes(x = lo, y = hi)) +
geom_point(mapping = aes(color = estimate), alpha = 0.5) +
geom_smooth(method = lm, color = 'red') +
xlim(0.0, 1.0) + ylim(0.0, 1.0)
```
But wait:
why is this complaining about missing values?
Some online searches lead to the discovery that
`geom_smooth` adds virtual points to the data for plotting purposes,
some of which lie outside the range of the actual data,
and that setting `xlim` and `ylim` then truncates these.
(Remember, R is differently sane…)
The safe way to control the range of the data is to add a call to `coord_cartesian`,
which effectively zooms in on a region of interest:
```{r plot-cartesian}
infant_hiv %>%
drop_na() %>%
filter(hi != 0.95) %>%
filter(!((lo < 0.10) & (hi > 0.25))) %>%
ggplot(mapping = aes(x = lo, y = hi)) +
geom_point(mapping = aes(color = estimate), alpha = 0.5) +
geom_smooth(method = lm, color = 'red') +
coord_cartesian(xlim = c(0.0, 1.0), ylim = c(0.0, 1.0))
```
## Do I need more practice with the tidyverse?
Absolutely:
open a fresh file and begin by loading the tidyverse
and the here package used to construct paths:
```{r fake-load-libraries, eval=FALSE}
library(tidyverse)
library(here)
```
Next,
use `here::here` to construct a path to a file and `readr::read_csv` to read that file:
```{r read-survey-data}
path = here::here("data", "person.csv")
person <- readr::read_csv(path)
```
We don't need to write out fully-qualified names—`here` and `read_csv` will do—but
we will use them to make it easier to see what comes from where.
Next,
have a look at the tibble `person`,
which contains some basic information about a group of foolhardy scientists
who ventured into the Antarctic in the 1920s and 1930s in search of things best left undisturbed:
```{r show-person}
person
```
How many rows and columns does this tibble contain?
```{r count-rows}
nrow(person)
```
```{r count-cols}
ncol(person)
```
(These names don't have a package prefix because they are built in.)
Let's show that information in a slightly nicer way
using `glue` to insert values into a string
and `print` to display the result:
```{r use-glue}
print(glue::glue("person has {nrow(person)} rows and {ncol(person)} columns"))
```
If we want to display several values,
we can use the function `paste` to combine the elements of a vector.
`colnames` gives us the names of a tibble's columns,
and `paste`'s `collapse` argument tells the function
to use a single space to separate concatenated values:
```{r use-colnames-and-paste}
print(glue::glue("person columns are {paste(colnames(person), collapse = ' ')}"))
```
Time for some data manipulation.
Let's get everyone's family and personal names:
```{r select-by-name}
dplyr::select(person, family_name, personal_name)
```
and then filter that list to keep only those that come in the first half of the alphabet:
```{r filter-with-two-conditions}
dplyr::select(person, family_name, personal_name) %>%
dplyr::filter(family_name < "N")
```
It would be more consistent to rewrite this as:
```{r filter-consistently}
person %>%
dplyr::select(family_name, personal_name) %>%
dplyr::filter(family_name < "N")
```
It's easy to add a column that records the lengths of family names:
```{r mutate-name-length}
person %>%
dplyr::mutate(name_length = stringr::str_length(family_name))
```
and then arrange in descending order:
```{r mutate-and-arrange}
person %>%
dplyr::mutate(name_length = stringr::str_length(family_name)) %>%
dplyr::arrange(dplyr::desc(name_length))
```
## Key Points
```{r keypoints, child="keypoints/tidyverse.md"}
```
```{r links, child="etc/links.md"}
```