This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.Rmd
358 lines (280 loc) · 9.66 KB
/
practice.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
# Practice Problems {#practice}
```{r setup, include=FALSE}
source("etc/common.R")
```
You need more practice with the functions in Chapter \@ref(tidyverse).
To begin,
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))
```
## Do I need even more practice?
Yes.
Yes, you do.
Let's load a slightly larger dataset:
```{r read-and-view}
measurements <- readr::read_csv(here::here("data", "measurements.csv"))
measurements
```
If we want an overview of our data's properties,
we can use the aptly-named `summarize` function:
```{r summarize}
dplyr::summarize(measurements)
```
Removing records with missing readings is straightforward:
```{r remove-reading-na}
measurements %>%
dplyr::filter(!is.na(reading))
```
Removing rows that contain *any* NAs is equally easy,
though it may be statistically unsound:
```{r hidden-cleaned, echo=FALSE}
cleaned <- measurements %>%
tidyr::drop_na()
```
We can now group our data by the quantity measured
and count the number of each—the column is named `n` automatically:
```{r group-by-quantity}
cleaned %>%
dplyr::group_by(quantity) %>%
dplyr::count()
```
How are the readings of each type distributed?
```{r min-ave-max}
cleaned %>%
dplyr::group_by(quantity) %>%
dplyr::summarize(low = min(reading),
mid = mean(reading),
high = max(reading))
```
After inspection,
we realize that most of the salinity measurements lie between 0 and 1,
but a handful range up to 100.
During a brief interval of lucidity,
the librarian who collected the battered notebooks from which the data was transcribed
informs us that one of the explorers recorded percentages rather than actual values.
We therefore decide to normalize all salinity measurements greater than 1.0 using `ifelse`
(a two-branch analog of `case_when`):
```{r rescale-salinity}
cleaned <- cleaned %>%
dplyr::mutate(reading = ifelse(quantity == 'sal' & reading > 1.0,
reading/100,
reading))
cleaned
```
To answer our next set of questions,
we need data about when each site was visited.
Let's read `visited.csv` and discard entries that are missing the visit date:
```{r read-visited}
visited <- readr::read_csv(here::here("data", "visited.csv")) %>%
dplyr::filter(!is.na(visit_date))
visited
```
and then combine that table with our cleaned measurement data.
We will use an [inner join](glossary.html#inner-join)
that matches records on the visit ID;
dplyr also provides other kinds of joins should we need them.
```{r join-two-tables}
combined <- visited %>%
dplyr::inner_join(cleaned,
by = c("visit_id" = "visit_id"))
```
We can now find the date of the highest radiation reading at each site:
```{r dates-high-rad}
combined %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(max_rad = max(reading)) %>%
dplyr::filter(reading == max_rad)
```
or:
```{r dates-high-rad-2}
combined %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::top_n(1, reading) %>%
dplyr::select(site_id, visit_date, reading)
```
The function `dplyr::lag` shifts the values in a column.
We can use it to calculate the difference in radiation at each site
between visits:
```{r rad-change}
combined %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(delta_rad = reading - dplyr::lag(reading)) %>%
dplyr::arrange(site_id, visit_date)
```
Going one step further,
we can create a list of sites at which radiation increased between any two visits:
```{r rad-increases}
combined %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(delta_rad = reading - dplyr::lag(reading)) %>%
dplyr::filter(!is.na(delta_rad)) %>%
dplyr::summarize(any_increase = any(delta_rad > 0)) %>%
dplyr::filter(any_increase)
```
## Please may I create some charts?
Certainly.
We will use data on the mass and home range area (HRA) of various species from:
> Tamburello N, Côté IM, Dulvy NK (2015) Data from: Energy and the scaling of animal space use. Dryad Digital Repository.
> https://doi.org/10.5061/dryad.q5j65
```{r read-hra}
hra <- readr::read_csv(here::here("data", "home-range-database.csv"))
head(hra)
```
A few keystrokes show us how the masses of these animals are distributed:
```{r chart-mass}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = mean.mass.g))
```
The distribution becomes much clearer if we plot the logarithms of the masses,
which are helpfully precalculated in `log10.mass`:
```{r chart-log-mass}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = log10.mass))
```
Let's tidy that up a bit:
```{r change-visual}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = log10.mass), bins = 100) +
ggplot2::ggtitle("Frequency of Species Masses") +
ggplot2::xlab("Log10 of Mass") +
ggplot2::ylab("Number of Species") +
ggplot2::theme_minimal()
```
How are mass and home range area related?
```{r scatterplot}
ggplot2::ggplot(hra) +
ggplot2::geom_point(mapping = aes(x = log10.mass, y = log10.hra))
```
Does the relationship depend on the class of animal?
(Here, we use the word "class" in the biological sense:
the class "aves" is birds.)
```{r colorize-scatterplot}
hra %>%
dplyr::mutate(class_fct = as.factor(class)) %>%
ggplot2::ggplot(mapping = aes(x = log10.mass, y = log10.hra, color = class_fct)) +
ggplot2::geom_point(alpha = 0.5)
```
> *What's a Factor?
>
> The code above creates a new column `class_fct`
> by converting the text values in `class` to a [factor](glossary.html#factor).
> Other languages call this an enumeration:
> we will discuss factors in more detail in Chapter \@ref(debt).
Our chart may be clearer if we display the [facets](glossary.html#facet) separately:
```{r facet-plot}
hra %>%
dplyr::mutate(class_fct = as.factor(class)) %>%
ggplot2::ggplot(mapping = aes(x = log10.mass, y = log10.hra, color = class_fct)) +
ggplot2::geom_point(alpha = 0.5) +
ggplot2::facet_wrap(~class_fct)
```
If we want to look at the mass-area relationship more closely for birds,
we can construct a regression line:
```{r fit-line}
hra %>%
dplyr::filter(class == "aves") %>%
ggplot2::ggplot(mapping = aes(x = log10.mass, y = log10.hra)) +
ggplot2::geom_point(alpha = 0.5) +
ggplot2::geom_smooth(method = lm, color = 'red')
```
Drilling down even further,
we can create a violin plot of mass by order for the birds
(where "order" is the biological division below "class"):
```{r violin-plot}
hra %>%
dplyr::filter(class == "aves") %>%
dplyr::mutate(order_fct = as.factor(order)) %>%
ggplot2::ggplot(mapping = aes(x = order_fct, y = log10.mass, color = order_fct)) +
ggplot2::geom_violin()
```
Changing just one line gives us a box plot instead:
```{r box-plot}
hra %>%
dplyr::filter(class == "aves") %>%
dplyr::mutate(order_fct = as.factor(order)) %>%
ggplot2::ggplot(mapping = aes(x = order_fct, y = log10.mass, color = order_fct)) +
ggplot2::geom_boxplot()
```
And if we want to save our chart to a file,
that's just one more call as well:
```{r save-file}
hra %>%
dplyr::filter(class == "aves") %>%
ggplot2::ggplot(mapping = aes(x = log10.mass, y = log10.hra)) +
ggplot2::geom_point(alpha = 0.5) +
ggplot2::geom_smooth(method = lm, color = 'red')
ggsave("/tmp/birds.png")
```
```{r links, child="etc/links.md"}
```