###################################################################
############### Module 6, Basics of data wrangling ################
###################################################################
### EXCERCISES (Lessons 10 and 11):

# 1) Load the package "tidyverse"
library(tidyverse)

# 2) Upload the dataset named "mammal_exc_vegetation.csv" directly into a tibble dataframe

mammal_exc_veg <- read_csv("data/mammal_exc_vegetation.csv")

# These data are based on a large scale large mammal exclosure experiment in an African savanna.
# The data we have here are based on vegetation surveys of all plant understory species
# present in m^2 subplots in a 7x7 grid in each plot.
# SURVEY = survey number/census
# YEAR, MONTH, DATE = the date surveyed
# SITE = the plot location (North (dry), Central (intermediate), South (wet))
# BLOCK = each site has three blocks (1-3)
# TREATMENT = OPEN = open plots (no exclosure)
#             MEGA = mega-herbivores excluded
#             MESO = meso- and mega-herbivores excluded
#             TOTAL = all herbivores excluded;
#             OUT = near to, but outside experimental plots (rarely used).
# PLOT = unique plot identifier combining SITE, BLOCK, and TREATMENT
# COORDINATE = indentifier for each m^2 subplot 

# More info and descriptions here:
# http://www.esapubs.org/archive/ecol/E095/064/#data
# Based on this citation:
# # Tyler R. Kartzinel, Jacob R. Goheen, Grace K. Charles, Elyse DeFranco, Janet E. Maclean, 
# Tobias O. Otieno, Todd M. Palmer, and Robert M. Pringle. 2014. Plant and small-mammal 
# responses to large-herbivore exclusion in an African savanna: five years of the UHURU
# experiment. Ecology 95:787.

# 3) Remove the columns of SURVEY, and YEAR and rename the non-species columns into a lower-case
# format and rename COORDINATE to something more meaningful (e.g., subplot)

mammal_exc_veg_clean <- select(mammal_exc_veg, -SURVEY, -YEAR, month=MONTH, site=SITE, block=BLOCK, treatment=TREATMENT,
                               plot=PLOT, subplot=COORDINATE, bare_ground=BARE_GROUND)

# 4) Filter the data to only OPEN and TOTAL treatments:
table(mammal_exc_veg_clean$treatment)

mammal_exc_veg_sub <- filter(mammal_exc_veg_clean, treatment == "TOTAL" | treatment == "OPEN")

# 5) Create a subset of the data that includes only species of the genus "Eragrostis." 
# Hint: you may need to use a more basic indexing method with the grepl function:

# basic method:
eragrostis_species <- names(mammal_exc_veg_sub)[grepl("Eragrostis", names(mammal_exc_veg_sub))]
mammal_exc_veg_erag <- mammal_exc_veg_sub[,c("month", "site", "block", "treatment", "plot", "subplot", "bare_ground", eragrostis_species)]

# using the select function (but note the warning message)
eragrostis_species <- names(mammal_exc_veg_sub)[grepl("Eragrostis", names(mammal_exc_veg_sub))]
mammal_exc_veg_erag <- select(mammal_exc_veg_sub, month:bare_ground, eragrostis_species)
# the message is because it is not clear to the select function whether "eragrostis_species" is a column
# or a separate vector as it is here.

# using the select function correctly:
mammal_exc_veg_erag <- select(mammal_exc_veg_sub, month:bare_ground, contains("eragrostis"))
# this is the easiest and most direct method, where we use a built in helper function called "contains"
#check out ?select to find other additional helper functions

# 6) Use the pipe "%>%" operator to combine your answers to questions 3, 4, and 5 into one
# script that selects/renames columns and filters the dataframe.
eragrostis_data <- select(mammal_exc_veg, -SURVEY, -YEAR, month=MONTH, site=SITE, block=BLOCK, treatment=TREATMENT,
                               plot=PLOT, subplot=COORDINATE, bare_ground=BARE_GROUND) %>% 
  filter(treatment == "TOTAL" | treatment == "OPEN") %>% 
  select(month:bare_ground, contains("eragrostis"))

eragrostis_data

# 7) Convert the Eragrostis species columns to either 0s or 1s indicating absence or presence. Remember
# that a "B" value indicates the species is present in a plot. Hint: you will need to use the mutate function.
names(eragrostis_data)
eragrostis_mod <- mutate(eragrostis_data, 
                         Eragrostis_papposa = as.numeric(Eragrostis_papposa=="B"),
                         Eragrostis_racemosa = as.numeric(Eragrostis_racemosa=="B"),
                         Eragrostis_rigidior = as.numeric(Eragrostis_rigidior=="B"),
                         Eragrostis_superba = as.numeric(Eragrostis_superba=="B"),
                         Eragrostis_spp = as.numeric(Eragrostis_spp=="B"),
                         Eragrostis_tenuifolia = as.numeric(Eragrostis_tenuifolia=="B"))

# but you could also use the across function with tools from the select function
# such as "contains", "starts_with", etc. First we can make a quick function to return
# 1s wherever a "B" occurs:
convert_spp_pres_to_numeric <- function(x) return(as.numeric(x=="B"))

eragrostis_mod <- mutate(eragrostis_data, 
                            across(contains("Eragrostis"), 
                            convert_spp_pres_to_numeric))


# 8) Use the split-apply-combine procedure to calculate the total number of Eragrostis_tenuifolia found
# within each site and treatment. Use the tibble from the previous question.
eragrostis_summary <- group_by(eragrostis_mod, site, treatment) %>% 
  summarize(total_E_tenuifolia = sum(Eragrostis_tenuifolia, na.rm=T)) %>% 
  ungroup()

# Great work! That's it in with these exercises--the next lesson is on combining dataframes using the
# tidyverse framework of functions.
