Load packages and read in datasets for class exercises:
# load csv file
herbivores <- read.csv(file = "data/coralreefherbivores.csv")
head(herbivores)
## family genus species gen.spe sl
## 1 Acanthuridae Acanthurus achilles Acanthurus.achilles 163.6667
## 2 Acanthuridae Acanthurus albipectoralis Acanthurus.albipectoralis 212.7300
## 3 Acanthuridae Acanthurus auranticavus Acanthurus.auranticavus 216.0000
## 4 Acanthuridae Acanthurus blochii Acanthurus.blochii 82.9000
## 5 Acanthuridae Acanthurus dussumieri Acanthurus.dussumieri 193.7033
## 6 Acanthuridae Acanthurus fowleri Acanthurus.fowleri 266.0000
## bodydepth snoutlength eyediameter size schooling
## 1 0.5543625 0.4877797 0.3507191 S Solitary
## 2 0.4405350 0.4402623 0.2560593 M SmallGroups
## 3 0.4726556 0.5386490 0.2451253 M MediumGroups
## 4 0.5586486 0.4782217 0.3196155 M SmallGroups
## 5 0.5457248 0.5661867 0.2807218 L Solitary
## 6 0.4669521 0.5950563 0.2217376 M Solitary
fish.abundance <- read.csv(file = "data/fish_abundance.csv")
head(fish.abundance)
## surveyid country site sitelat sitelong surveydate depth
## 1 4000720 Australia Watsons Bay north -14.66 145.45 6/27/10 14:00 3
## 2 4000720 Australia Watsons Bay north -14.66 145.45 6/27/10 14:00 3
## 3 4000720 Australia Watsons Bay north -14.66 145.45 6/27/10 14:00 3
## 4 4000720 Australia Watsons Bay north -14.66 145.45 6/27/10 14:00 3
## 5 4000720 Australia Watsons Bay north -14.66 145.45 6/27/10 14:00 3
## 6 4000720 Australia Watsons Bay north -14.66 145.45 6/27/10 14:00 3
## family genus species block total genspe
## 1 Acanthuridae Acanthurus grammoptilus 2 4 Acanthurus.grammoptilus
## 2 Acanthuridae Acanthurus nigricauda 2 3 Acanthurus.nigricauda
## 3 Acanthuridae Acanthurus olivaceus 2 2 Acanthurus.olivaceus
## 4 Acanthuridae Ctenochaetus binotatus 1 15 Ctenochaetus.binotatus
## 5 Acanthuridae Ctenochaetus binotatus 2 10 Ctenochaetus.binotatus
## 6 Acanthuridae Zebrasoma scopas 1 2 Zebrasoma.scopas
The “coralreefherbivores.csv” dataset contains trait information on 93 species of herbivorous coral reef fishes from the Great Barrier Reef, Australia. The first four columns provide taxonomic information (family, genus, species, and a combined genus + species column called “genspe”). The next column “sl” indicates the standard length of the fish. Then, you have three columns with morphological measurements of body depth, snout length, and eye diameter. Finally, you have two columns that indicate maximum body size (categories from XS to XL) and their schooling behavior.
# 1) Determine the mean standard length and standard deviation for species in the families Acanthuridae, Labridae, and Siganidae.
ex1 <- herbivores %>%
group_by(family) %>%
filter(family != "Kyphosidae") %>%
summarize(meanSL = mean(sl), sdSL = sd(sl))
ex1
## # A tibble: 3 × 3
## family meanSL sdSL
## <chr> <dbl> <dbl>
## 1 Acanthuridae 186. 85.0
## 2 Labridae 247. 81.3
## 3 Siganidae 161. 53.9
# 2) Create a variable that combines size and schooling into one column.
ex2 <- herbivores %>%
mutate(size_school = paste(size, schooling, sep = "_"))
ex2$size_school
## [1] "S_Solitary" "M_SmallGroups" "M_MediumGroups" "M_SmallGroups"
## [5] "L_Solitary" "M_Solitary" "S_MediumGroups" "M_Solitary"
## [9] "S_SmallGroups" "M_Solitary" "M_Solitary" "M_SmallGroups"
## [13] "S_Solitary" "M_Solitary" "S_Solitary" "S_Solitary"
## [17] "S_Solitary" "M_Solitary" "S_Solitary" "S_Solitary"
## [21] "S_MediumGroups" "L_MediumGroups" "XL_MediumGroups" "L_Solitary"
## [25] "S_Solitary" "XL_SmallGroups" "M_SmallGroups" "M_Solitary"
## [29] "M_MediumGroups" "M_SmallGroups" "L_SmallGroups" "S_Solitary"
## [33] "XS_Solitary" "S_Solitary" "S_Solitary" "S_Solitary"
## [37] "XS_Solitary" "S_Solitary" "L_MediumGroups" "L_SmallGroups"
## [41] "M_Solitary" "XL_SmallGroups" "XL_SmallGroups" "L_SmallGroups"
## [45] "M_Solitary" "L_LargeGroups" "M_Solitary" "L_MediumGroups"
## [49] "M_SmallGroups" "L_SmallGroups" "L_SmallGroups" "L_SmallGroups"
## [53] "M_SmallGroups" "L_MediumGroups" "M_Solitary" "M_SmallGroups"
## [57] "M_Solitary" "M_Solitary" "L_Solitary" "M_SmallGroups"
## [61] "XL_Solitary" "M_MediumGroups" "M_SmallGroups" "M_Solitary"
## [65] "M_SmallGroups" "M_SmallGroups" "L_MediumGroups" "S_MediumGroups"
## [69] "M_MediumGroups" "M_MediumGroups" "L_Solitary" "M_MediumGroups"
## [73] "S_MediumGroups" "S_Solitary" "L_Solitary" "M_SmallGroups"
## [77] "S_MediumGroups" "S_Pairs" "S_Pairs" "M_MediumGroups"
## [81] "M_SmallGroups" "L_SmallGroups" "M_SmallGroups" "S_Pairs"
## [85] "M_Pairs" "M_Pairs" "M_Pairs" "S_SmallGroups"
## [89] "S_SmallGroups" "M_Pairs" "M_SmallGroups" "S_Pairs"
## [93] "S_SmallGroups" "S_Solitary" "M_Pairs" "M_Pairs"
ex2.alt <- herbivores %>%
unite("size_school", size, schooling, sep = "_")
ex2.alt$size_school
## [1] "S_Solitary" "M_SmallGroups" "M_MediumGroups" "M_SmallGroups"
## [5] "L_Solitary" "M_Solitary" "S_MediumGroups" "M_Solitary"
## [9] "S_SmallGroups" "M_Solitary" "M_Solitary" "M_SmallGroups"
## [13] "S_Solitary" "M_Solitary" "S_Solitary" "S_Solitary"
## [17] "S_Solitary" "M_Solitary" "S_Solitary" "S_Solitary"
## [21] "S_MediumGroups" "L_MediumGroups" "XL_MediumGroups" "L_Solitary"
## [25] "S_Solitary" "XL_SmallGroups" "M_SmallGroups" "M_Solitary"
## [29] "M_MediumGroups" "M_SmallGroups" "L_SmallGroups" "S_Solitary"
## [33] "XS_Solitary" "S_Solitary" "S_Solitary" "S_Solitary"
## [37] "XS_Solitary" "S_Solitary" "L_MediumGroups" "L_SmallGroups"
## [41] "M_Solitary" "XL_SmallGroups" "XL_SmallGroups" "L_SmallGroups"
## [45] "M_Solitary" "L_LargeGroups" "M_Solitary" "L_MediumGroups"
## [49] "M_SmallGroups" "L_SmallGroups" "L_SmallGroups" "L_SmallGroups"
## [53] "M_SmallGroups" "L_MediumGroups" "M_Solitary" "M_SmallGroups"
## [57] "M_Solitary" "M_Solitary" "L_Solitary" "M_SmallGroups"
## [61] "XL_Solitary" "M_MediumGroups" "M_SmallGroups" "M_Solitary"
## [65] "M_SmallGroups" "M_SmallGroups" "L_MediumGroups" "S_MediumGroups"
## [69] "M_MediumGroups" "M_MediumGroups" "L_Solitary" "M_MediumGroups"
## [73] "S_MediumGroups" "S_Solitary" "L_Solitary" "M_SmallGroups"
## [77] "S_MediumGroups" "S_Pairs" "S_Pairs" "M_MediumGroups"
## [81] "M_SmallGroups" "L_SmallGroups" "M_SmallGroups" "S_Pairs"
## [85] "M_Pairs" "M_Pairs" "M_Pairs" "S_SmallGroups"
## [89] "S_SmallGroups" "M_Pairs" "M_SmallGroups" "S_Pairs"
## [93] "S_SmallGroups" "S_Solitary" "M_Pairs" "M_Pairs"
# 3) Examine the mean eye diameter of the genera Zebrasoma and Scarus.
ex3 <- herbivores %>%
group_by(genus) %>%
summarize(mean.eye = mean(eyediameter)) %>%
filter(genus == "Zebrasoma" | genus == "Scarus")
ex3
## # A tibble: 2 × 2
## genus mean.eye
## <chr> <dbl>
## 1 Scarus 0.195
## 2 Zebrasoma 0.305
# 4) Count the number of species in each size class.
ex4 <- herbivores %>%
group_by(size) %>%
summarize(n = n())
ex4
## # A tibble: 5 × 2
## size n
## <chr> <int>
## 1 L 18
## 2 M 43
## 3 S 28
## 4 XL 5
## 5 XS 2
# 5) Calculate the ratio of snout length to standard length for each species, then determine which genus has the longest mean ratio of snout length to standard length.
ex5 <- herbivores %>%
mutate(snoutlength_sl_ratio = snoutlength/sl) %>%
group_by(genus) %>%
summarize(mean_ratio = mean(snoutlength_sl_ratio)) %>%
arrange(-mean_ratio)
ex5
## # A tibble: 14 × 2
## genus mean_ratio
## <chr> <dbl>
## 1 Zebrasoma 0.00519
## 2 Paracanthurus 0.00509
## 3 Ctenochaetus 0.00492
## 4 Acanthurus 0.00320
## 5 Siganus 0.00239
## 6 Naso 0.00216
## 7 Calotomus 0.00204
## 8 Chlorurus 0.00161
## 9 Scarus 0.00151
## 10 Leptoscarus 0.00137
## 11 Cetoscarus 0.00116
## 12 Bolbometopon 0.00102
## 13 Hipposcarus 0.00101
## 14 Kyphosus 0.000574
The “fish_abundance.csv” dataset contains reef fish abundance information, which was collected by the Reef Life Survey around Lizard Island, Australia. It includes information on the respective survey ID and its metadata (e.g. site, latitude, longitude, date, and depth). It also includes a row for each observed species on a given visual survey, including taxonomic information (family, genus, species, and “genspe”), as well as the number of individuals observed on that survey (‘total’).
fish.filtered <- fish.abundance %>%
mutate(family = recode(family, Scaridae = "Labridae")) %>%
filter(family %in% c("Acanthuridae", "Siganidae", "Kyphosidae", "Labridae")) %>%
group_by(family, genus) %>%
summarize(mean.abun = mean(total))
fish.filtered
## # A tibble: 33 × 3
## # Groups: family [4]
## family genus mean.abun
## <chr> <chr> <dbl>
## 1 Acanthuridae Acanthurid 1
## 2 Acanthuridae Acanthurus 4.01
## 3 Acanthuridae Ctenochaetus 5.58
## 4 Acanthuridae Naso 1.54
## 5 Acanthuridae Paracanthurus 2
## 6 Acanthuridae Zebrasoma 2.80
## 7 Kyphosidae Kyphosus 1.5
## 8 Labridae Anampses 3.33
## 9 Labridae Bodianus 1.32
## 10 Labridae Calotomus 1
## # … with 23 more rows
herbivore.traits <- herbivores %>%
select(-size, -schooling, -sl) %>%
gather(5:7, key = trait, value = value) %>%
group_by(family, genus, trait) %>%
summarize(mean_trait = mean(value)) %>%
spread(4, key = trait, value = mean_trait) %>%
left_join(fish.filtered) %>%
drop_na(mean.abun)
herbivore.traits
## # A tibble: 11 × 6
## # Groups: family, genus [11]
## family genus bodydepth eyediameter snoutlength mean.abun
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Acanthuridae Acanthurus 0.508 0.298 0.498 4.01
## 2 Acanthuridae Ctenochaetus 0.532 0.295 0.535 5.58
## 3 Acanthuridae Naso 0.386 0.274 0.547 1.54
## 4 Acanthuridae Paracanthurus 0.490 0.295 0.533 2
## 5 Acanthuridae Zebrasoma 0.575 0.305 0.657 2.80
## 6 Kyphosidae Kyphosus 0.479 0.181 0.164 1.5
## 7 Labridae Calotomus 0.385 0.246 0.306 1
## 8 Labridae Cetoscarus 0.387 0.139 0.428 1
## 9 Labridae Chlorurus 0.400 0.187 0.366 5.39
## 10 Labridae Scarus 0.391 0.195 0.341 3.42
## 11 Siganidae Siganus 0.443 0.329 0.352 2.21