forked from cambiotraining/IntroR_November5-6
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode-handout.R
executable file
·593 lines (366 loc) · 14.2 KB
/
code-handout.R
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
### Creating objects in R
### Challenge
##
## What are the values after each statement in the following?
##
## mass <- 47.5 # mass?
## age <- 122 # age?
## mass <- mass * 2.0 # mass?
## age <- age - 20 # age?
## mass_index <- mass/age # mass_index?
### Vectors and data types
## ## We’ve seen that atomic vectors can be of type character, numeric, integer, and
## ## logical. But what happens if we try to mix these types in a single
## ## vector?
##
## ## What will happen in each of these examples? (hint: use `class()` to
## ## check the data type of your object)
## num_char <- c(1, 2, 3, "a")
##
## num_logical <- c(1, 2, 3, TRUE)
##
## char_logical <- c("a", "b", "c", TRUE)
##
## tricky <- c(1, 2, 3, "4")
##
## ## Why do you think it happens?
##
## ## You've probably noticed that objects of different types get
## ## converted into a single, shared type within a vector. In R, we call
## ## converting objects from one class into another class
## ## _coercion_. These conversions happen according to a hierarchy,
## ## whereby some types get preferentially coerced into other types. Can
## ## you draw a diagram that represents the hierarchy of how these data
## ## types are coerced?
### Challenge (optional)
##
## * Can you figure out why `"four" > "five"` returns `TRUE`?
heights <- c(63, 69, 60, 65, NA, 68, 61, 70, 61, 59, 64, 69, 63, 63, NA, 72, 65, 64, 70, 63, 65)
# 1.
heights_no_na <- heights[!is.na(heights)]
# or
heights_no_na <- na.omit(heights)
# or
heights_no_na <- heights[complete.cases(heights)]
# 2.
median(heights, na.rm = TRUE)
# 3.
heights_above_67 <- heights_no_na[heights_no_na > 67]
length(heights_above_67)
## ### Challenge
## 1. Using this vector of heights in inches, create a new vector with the NAs removed.
##
## heights <- c(63, 69, 60, 65, NA, 68, 61, 70, 61, 59, 64, 69, 63, 63, NA, 72, 65, 64, 70, 63, 65)
##
## 2. Use the function `median()` to calculate the median of the `heights` vector.
##
## 3. Use R to figure out how many people in the set are taller than 67 inches.
### Presentation of the survey data
## download.file(url="https://ndownloader.figshare.com/files/2292169",
## destfile = "data_raw/portal_data_joined.csv")
str(surveys)
## * class: data frame
## * how many rows: 34786, how many columns: 13
## * how many taxa: 4
## # get all the records that have species as "albigula"
## surveys[surveys$species == "albigula",]
## # save all the records that have species as "albigula" into a variable
## albigula_data <- surveys[surveys$species == "albigula",]
## # how many records have species as "albigula" in the surveys data frame?
## nrow(albigula_data)
### Factors
sex <- factor(c("male", "female", "female", "male"))
year_fct <- factor(c(1990, 1983, 1977, 1998, 1990))
as.numeric(year_fct) # Wrong! And there is no warning...
as.numeric(as.character(year_fct)) # Works...
as.numeric(levels(year_fct))[year_fct] # The recommended way.
## bar plot of the number of females and males captured during the experiment:
plot(surveys$sex)
## Challenges
##
## * Rename "F" and "M" to "female" and "male" respectively.
## * Now that we have renamed the factor level to "undetermined", can you recreate the
## barplot such that "undetermined" is last (after "male")
## ## Compare the difference between our data read as `factor` vs `character`.
## surveys <- read.csv("data_raw/portal_data_joined.csv", stringsAsFactors = TRUE)
## str(surveys)
## surveys <- read.csv("data_raw/portal_data_joined.csv", stringsAsFactors = FALSE)
## str(surveys)
## ## Convert the column "plot_type" into a factor
## surveys$plot_type <- factor(surveys$plot_type)
## ## Challenge:
## ## There are a few mistakes in this hand-crafted `data.frame`,
## ## can you spot and fix them? Don't hesitate to experiment!
## animal_data <- data.frame(
## animal = c(dog, cat, sea cucumber, sea urchin),
## feel = c("furry", "squishy", "spiny"),
## weight = c(45, 8 1.1, 0.8)
## )
## ## Challenge:
## ## Can you predict the class for each of the columns in the following
## ## example?
## ## Check your guesses using `str(country_climate)`:
## ## * Are they what you expected? Why? why not?
## ## * What would have been different if we had added `stringsAsFactors = FALSE`
## ## when we created this data frame?
## ## * What would you need to change to ensure that each column had the
## ## accurate data type?
## country_climate <- data.frame(country = c("Canada", "Panama", "South Africa", "Australia"),
## climate = c("cold", "hot", "temperate", "hot/temperate"),
## temperature = c(10, 30, 18, "15"),
## northern_hemisphere = c(TRUE, TRUE, FALSE, "FALSE"),
## has_kangaroo = c(FALSE, FALSE, FALSE, 1))
# R Base
surveys[surveys$species == "albigula" &
surveys$year == 1977, ]
# Tidyverse
filter(surveys, species == "albigula", year == 1977)
# Load the tidyverse package
library(tidyverse)
surveys <- read_csv("data_raw/portal_data_joined.csv")
# Print the first 15 rows
print(surveys, n = 15)
# Inspect the data
str(surveys)
ggplot(data = surveys, mapping = aes(x = weight, y = hindfoot_length))
ggplot(data = surveys, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point()
ggplot(data = surveys, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1)
ggplot(data = surveys, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, colour = "blue")
# Assign plot to a variable
surveys_plot <- ggplot(data = surveys,
mapping = aes(x = weight, y = hindfoot_length))
# Draw a scatter plot
surveys_plot +
geom_point()
surveys_plot +
geom_smooth()
surveys_plot +
geom_point() +
geom_smooth()
#save plot that you would like to save into a variable
out_plot <- surveys_plot + geom_density2d()
#save plot to file
ggsave(filename="img_output/plot_weight_hindfoot_density2d.png", plot = out_plot)
# Save plot to file
ggsave(filename = "img_output/plot_weight_hindfoot_density2d.png")
## # Extract species_id, weight, hindfoot_lenth, year and sex columns from surveys dataset.
## select(surveys, species_id, weight, hindfoot_length, year, sex)
## # Select all columns of the surveys dataset apart from record_id and species_id columns.
## select(surveys, -record_id, -species_id)
## # Keep only the observations of animals collected from 1995 onwards from the surveys dataset.
## filter(surveys, year >= 1995)
## # Keep only the observations of animals collected from 1995 onwards
## # that are female from the surveys dataset.
## filter(surveys, year >= 1995, sex == "F")
# Assign plot to a variable
surveys_plot <- ggplot(data = surveys,
mapping = aes(x = weight, y = hindfoot_length))
# Draw a scatter plot
surveys_plot +
geom_point()
## # Which values of the weight column are missing?
## is.na(surveys$weight)
##
## # Which values of the weight column are not missing?
## !is.na(surveys$weight)
## filter(surveys,
## !is.na(weight), # remove rows that have weight as NA
## !is.na(hindfoot_length)) # remove rows that have hindfoot_length as NA
drop_na(surveys, weight, hindfoot_length)
drop_na(surveys)
surveys_complete <- drop_na(surveys)
## surveys2 <- select(surveys_complete, species_id, weight, hindfoot_length, year, sex)
## surveys_recent <- filter(surveys2, year >= 1995)
## surveys_recent <- filter(
## select(surveys_complete,
## species_id, weight, hindfoot_length, year, sex)
## , year >= 1995)
## surveys_complete %>%
## select(species_id, weight, hindfoot_length, year, sex) %>%
## filter(year >= 1995)
## surveys_recent <- surveys_complete %>%
## select(species_id, weight, hindfoot_length, year, sex) %>%
## filter(year >= 1995)
##
## surveys_recent
surveys_complete %>%
# Select columns
select(species_id, weight, hindfoot_length, year, sex) %>%
# Filter rows
filter(year >= 1995) %>%
# Plot transformed data
ggplot(mapping = aes(x = weight, y = hindfoot_length)) +
geom_point()
survey_recent <- surveys_complete %>%
select(species_id, weight, hindfoot_length, year, sex) %>%
filter(year >= 1995)
ggplot(mapping = aes(x = weight, y = hindfoot_length)) +
geom_point(data = surveys_complete) +
geom_point(data = survey_recent, colour = "red")
surveys_complete %>%
# Add weight_kg column
mutate(weight_kg = weight / 1000) %>%
# Select columns
select(species_id, weight_kg, hindfoot_length, year, sex) %>%
# Filter rows
filter(year >= 1995) %>%
# Plot transformed data
ggplot(mapping = aes(x = year, y = weight_kg)) +
geom_boxplot()
surveys_complete %>%
# Add weight_kg column
mutate(weight_kg = weight / 1000) %>%
# Select columns
select(species_id, weight_kg, hindfoot_length, year, sex) %>%
# Filter rows
filter(year >= 1995) %>%
# Convert the year column to a factor
mutate(year = as_factor(year)) %>%
# Plot transformed data
ggplot(mapping = aes(x = year, y = weight_kg)) +
geom_boxplot()
surveys_complete %>%
# Add weight_kg column
mutate(weight_kg = weight / 1000) %>%
# Select columns
select(species_id, weight_kg, hindfoot_length, year, sex) %>%
# Filter rows
filter(year >= 1995) %>%
# Convert the year variable and plot transformed data
ggplot(mapping = aes(x = as_factor(year), y = weight_kg)) +
geom_boxplot()
# Sort weight in ascending order (default)
surveys_complete %>%
arrange(weight)
library(rmarkdown)
paged_table(surveys_complete %>% arrange(weight))
# Sort weight in descending order
surveys_complete %>%
arrange(desc(weight))
library(rmarkdown)
paged_table(surveys_complete %>% arrange(desc(weight)))
# Sort weight in ascending order and hindfoot_length in descending order
surveys_complete %>%
arrange(weight, desc(hindfoot_length))
library(rmarkdown)
paged_table(surveys_complete %>% arrange(weight, desc(hindfoot_length)))
surveys_complete %>%
mean_weight = mean(weight)
surveys_complete %>%
summarise(mean_weight = mean(weight))
surveys_complete %>%
count(sex)
surveys_complete %>%
count(sex, species)
surveys_complete %>%
count(plot_type)
surveys_complete %>%
count(sex, species) %>%
arrange(species, desc(n))
yearly_counts <- surveys_complete %>%
count(year, genus)
yearly_counts
ggplot(surveys_complete, aes(weight)) +
geom_histogram()
ggplot(surveys_complete, aes(weight, fill = sex)) +
geom_histogram(bins = 100)
ggplot(surveys_complete, aes(weight, colour = sex)) +
geom_freqpoly()
surveys_complete %>%
# Extract females
filter(sex == "F") %>%
summarise(mean_weight = mean(weight))
surveys_complete %>%
# Extract males
filter(sex == "M") %>%
summarise(mean_weight = mean(weight))
surveys_complete %>%
group_by(sex) %>%
summarise(mean_weight = mean(weight))
surveys_complete %>%
group_by(sex, species_id) %>%
summarise(mean_weight = mean(weight))
surveys_complete %>%
group_by(sex, species_id) %>%
summarise(mean_weight = mean(weight),
min_weight = min(weight))
surveys_complete %>%
group_by(species_id) %>%
summarise(
mean_hindfoot_length = mean(hindfoot_length),
min_hindfoot_length = min(hindfoot_length),
max_hindfoot_length = max(hindfoot_length),
n_obs = n()
)
surveys_complete %>%
group_by(year) %>%
filter(weight == max(weight)) %>%
select(year, genus, species_id, weight) %>%
arrange(year)
ggplot(data = yearly_counts, mapping = aes(x = year, y = n)) +
geom_line() +
facet_wrap(facets = vars(genus))
ggplot(data = yearly_counts, mapping = aes(x = year, y = n)) +
geom_line() +
facet_wrap(facets = vars(genus), scales = "free")
yearly_sex_counts <- surveys_complete %>%
count(year, genus, sex)
yearly_sex_counts
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, colour = sex)) +
geom_line() +
facet_wrap(facets = vars(genus))
ggplot(data = yearly_counts, mapping = aes(x = year, y = n)) +
geom_line() +
# Display the genera as columns
facet_grid(cols = vars(genus))
ggplot(data = yearly_sex_counts,
mapping = aes(x = year, y = n)) +
geom_line() +
facet_grid(rows = vars(sex), cols = vars(genus))
ggplot(data = yearly_sex_counts,
mapping = aes(x = year, y = n, colour = sex)) +
geom_line() +
facet_grid(cols = vars(genus))
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n)) +
geom_line() +
facet_grid(cols = vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of animals")
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, colour = sex)) +
geom_line() +
facet_grid(cols = vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of animals") +
theme(axis.text.x = element_text(size = 7, angle = 90, vjust = 0.5),
axis.text.y = element_text(size = 7),
strip.text=element_text(size = 7, angle = 45))
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, colour = sex)) +
geom_line() +
facet_grid(cols = vars(genus)) +
labs(title = "Observed genera through time",
x = "Year of observation",
y = "Number of animals") +
theme(axis.text.x = element_text(size = 7, angle = 90, vjust = 0.5),
axis.text.y = element_text(size = 7),
strip.text = element_text(size = 7, angle = 45)) +
scale_colour_brewer("Sex",
palette = "Set1",
breaks = c("F", "M"),
labels = c("Female", "Male"))
ggplot(data = surveys, mapping = aes(x = weight, y = hindfoot_length)) +
geom_point() +
theme_bw()
surveys_complete %>%
group_by(year, species) %>%
summarise(avg_weight = mean(weight)) %>%
ggplot(mapping = aes(x = year, y = avg_weight)) +
geom_line() +
facet_wrap(facets = vars(species)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
write_csv(surveys_complete, path = "data_output/surveys_complete.csv")