###################################################################
############### Module 6, Basics of data wrangling ################
###################################################################
### EXCERCISES (Lessons 12 and 13):

# First here is the code to load and cleanup the mammal exclosure vegetation data from the previous exercise file
# "mammal_exc_vegetation.csv":
library(tidyverse)
mammal_exc_veg <- read_csv("data/mammal_exc_vegetation.csv")

veg_data <- select(mammal_exc_veg, year = YEAR, month=MONTH, site=SITE, block=BLOCK, treatment=TREATMENT,
                          plot=PLOT, subplot=COORDINATE, Abutilon:unknown_F) %>% 
  filter(treatment == "TOTAL" | treatment == "OPEN") %>% 
  select(year:subplot, everything())

### Quick reminder of what the columns mean:
# month = the month when the data were collected
# site = South, central, or north site
# block = 1, 2, or 3 (each site has 3 blocks)
# treatment = OPEN = open plots (no exclosure)
#             MEGA = megaherbivores excluded
#             MESO = meso- and megaherbivores excluded
#             TOTAL = all herbivores excluded;
#             OUT = near to, but outside experimental plots (rarely used).
# plot = unique plot identifier combining SITE, BLOCK, and TREATMENT
# sublot = marker for each of the 49 subplot gridpoint locations in each olot
# bare_ground = presence (B) or absence (0) of bare ground at that m^2 sublot location
# species = presence (B) or absence (0) of that species at that m^2 sublot location


# Now also load the mammal exclosure habitat data "mammal_exc_habitat.csv":
mammal_exc_hab <- read_csv("data/mammal_exc_habitat.csv")

# The columns in this dataset:
# LEVEL = same as site (south, central, north)
# BLOCK = same as block
# TREATMENT = same as treatment
# PLOT = same as plot
# REBAR = same as subplot
# TREE_CANOPY = Describes canopy cover above the subplot/rebar: 
#        Under = fully under canopy; by_under = under canopy, but near edge; 
#        by = at canopy edge; by_open = beside a tree canopy; 
#        open = not under or near a canopy.
# THICKET = When rebar is under or next to a tree, is it part of a thicket? Y/N (N if not near tree)
# HERBACEOUS = Describes density of herbaceous cover around the rebar (seven levels):
#        Bare = no herbaceous cover; few-bare, few, med-few, med, med-high, and high are increasing levels of cover.
# SLOPE = angle of the ground (flat, low, med, steep)
# ASPECT = Cardinal direction that the slope faces
# STONY, SANDY = substrate characters (Y/N)
# WATER_CHANNEL = Proximity of rebar to surface water (11 classes):
#       N = no water association
#       by = beside a surface water runoff channel
#       in = within a surface water runoff channel.
#       For rebar by or in a surface water runoff channel, qualifiers include:
#       Flow = water runs over the area without a distinct channel.
#       Shallow, med, and deep = levels of channel depth.
# species names = which tree species are part of the canopy cover:
#       Y = yes; N = No. Close = rebar in the open, but a canopy is nearby (1–2 m)
############################
# Exercises: 

# 1) Since the vegetation surveys were conducted through repeated surveys, filter the vegetation
# dataframe to only the final year of data:


# 2) Create a case_when function that takes in a species presence value (0, 'B', or NA),
# and converts those to (0, 1, or 0 (NAs are 0s)):


# 3) Use this function to convert all species presence columns:
# Hint: use the across function:


# 4) Since there are 49 subplot observations for each plot, summarize those all by plot
# to create a frequency of occurrence of each species in each plot. Hint, you will need to
# use the group_by, summarize, and across functions. Another hint is that the mean of all
# 0 or 1 occurrence values is the same as its frequency:


# 5) Now go back and clean up the habitat data so that the columns in common with the vegetation data
# have the same names (case sensitive) and remove the tree species columns:


# 6) Filter the habitat data to only those treatments that were either "OPEN" or "TOTAL" exclosure.
# Also remove the columns thicket through water_channel:


# 7) Your next challenge is to summarize the tree_canopy, stony, and sandy columns by plot.
# Can you figure out a clever way to turn those variables into numeric summaries?
# Be sure to check out the solutions video on this exercise file to see an example
# solution if you are stumped on this one. Hint: keep the site, block, and treatment
# columns by also including them in the group_by function.


# 8) Join the vegetation summary data from question 4 to the habitat summary data from the previous question:


# 9) Create a species X plot matrix using the vegetation frequency data from question 4:


# 10) Some species were not found in any plot for some reason, so remove all the species
# columns where the species had zero occurrences in the matrix. Hint: since it's a matrix
# you'll have to rely on some old-school indexing and the apply function.


# 11) Which species has the highest frequency in any one plot?


# 12) Which plot has the greatest number of species?


# 13) Create a scatterplot showing how the species richness in each plot is related to the 
# relative proportion of overstory trees in the habitat data, and a boxplot showing the 
# relationship between species richness and site.


# That's it!

