library(readr)
# URLs for datasets
<- "https://raw.githubusercontent.com/cbrown5/example-ecological-data/refs/heads/main/data/benthic-reefs-and-fish/benthic_cover.csv"
benthic_cover_url <- "https://raw.githubusercontent.com/cbrown5/example-ecological-data/refs/heads/main/data/benthic-reefs-and-fish/benthic_variables.csv"
benthic_variables_url <- "https://raw.githubusercontent.com/cbrown5/example-ecological-data/refs/heads/main/data/benthic-reefs-and-fish/fish-coral-cover-sites.csv"
fish_coral_cover_sites_url
# Local file paths
<- "data/benthic_cover.csv"
benthic_cover_path <- "data/benthic_variables.csv"
benthic_variables_path <- "data/fish-coral-cover-sites.csv"
fish_coral_cover_sites_path
# Download and save datasets
<- read_csv(benthic_cover_url)
benthic_cover write_csv(benthic_cover, benthic_cover_path)
<- read_csv(benthic_variables_url)
benthic_variables write_csv(benthic_variables, benthic_variables_path)
<- read_csv(fish_coral_cover_sites_url)
fish_coral_cover_sites write_csv(fish_coral_cover_sites, fish_coral_cover_sites_path)
14 Code for fish and benthic analysis
14.1 Read in the data from an online source
14.2 Convert the benthic data to a wide format data frame
# Load required libraries
library(tidyverse)
# Load benthic cover data
<- read_csv("data/benthic_cover.csv")
benthic
# Calculate proportional cover per transect
<- benthic %>%
benthic mutate(prop_cover = cover / n.pts)
# Average proportional cover by site and habitat type
<- benthic %>%
benthic_site group_by(site, code) %>%
summarise(mean_prop_cover = mean(prop_cover, na.rm = TRUE), .groups = "drop")
# Pivot to wide format: one row per site, columns for each habitat type
<- benthic_site %>%
benthic_wide pivot_wider(names_from = code, values_from = mean_prop_cover)
# View result
print(head(benthic_wide))
::write_csv(benthic_wide, file = "data/benthic_wide.csv") readr
14.3 Join the benthic and fish datasets
library(tidyverse)
library(readr)
<- read_csv("data/fish-coral-cover-sites.csv")
fish <- read_csv("data/benthic_wide.csv")
benthic_wide
<- left_join(fish, benthic_wide, by = "site") dat