###################################################################
############### Module 6, Basics of data wrangling ################
###################################################################
## Lesson 13, Part 1: Reshaping your data and some other useful functions (recoding)

# First load our island plant data and tidyverse packages:
library(tidyverse)
island_plants <- read_csv("data/island_veg_data_raw.csv")

###################################
##### RECODING VARIABLES
# A useful method for changing the values of variables:

island_cover_data <- filter(island_plants, CoverType=="midstory" | CoverType=="understory") %>% 
  select(island=Island, plot=Plot, date=`Date (mm/dd/yy)`, species=Species,
         covertype = CoverType,
         cover_mid = Over_Midstory,
         cover_n = `Cover N`,
         cover_e = `Cover E`,
         cover_s = `Cover S`,
         cover_w = `Cover W`)

midstory_cover <- island_cover_data$cover_mid

# Cover classes represent these percent covers:
# 1 = <5%       0.5
# 2 = 6-15%     10.5
# 3 = 16-25%    20.5
# 4 = 26-50%    38
# 5 = 51-75%    63
# 6 = 76-100%   88

# midstory_cover <- ifelse(midstory_cover==1, 0.5, midstory_cover)

midstory_converted <- case_when(midstory_cover == 1 ~ 0.5,
                                midstory_cover == 2 ~ 10.5,
                                midstory_cover == 3 ~ 20.5,
                                midstory_cover == 4 ~ 38,
                                midstory_cover == 5 ~ 63,
                                midstory_cover == 6 ~ 88)

covers_converted <- mutate(island_cover_data,
                           cover_mid = case_when(cover_mid == 1 ~ 0.5,
                                                 cover_mid == 2 ~ 10.5,
                                                 cover_mid == 3 ~ 20.5,
                                                 cover_mid == 4 ~ 38,
                                                 cover_mid == 5 ~ 63,
                                                 cover_mid == 6 ~ 88),
                           cover_n = case_when(cover_n == 1 ~ 0.5,
                                               cover_n == 2 ~ 10.5,
                                               cover_n == 3 ~ 20.5,
                                               cover_n == 4 ~ 38,
                                               cover_n == 5 ~ 63,
                                               cover_n == 6 ~ 88),
                           cover_e = case_when(cover_e == 1 ~ 0.5,
                                               cover_e == 2 ~ 10.5,
                                               cover_e == 3 ~ 20.5,
                                               cover_e == 4 ~ 38,
                                               cover_e == 5 ~ 63,
                                               cover_e == 6 ~ 88),
                           cover_s = case_when(cover_s == 1 ~ 0.5,
                                               cover_s == 2 ~ 10.5,
                                               cover_s == 3 ~ 20.5,
                                               cover_s == 4 ~ 38,
                                               cover_s == 5 ~ 63,
                                               cover_s == 6 ~ 88),
                           cover_w = case_when(cover_w == 1 ~ 0.5,
                                               cover_w == 2 ~ 10.5,
                                               cover_w == 3 ~ 20.5,
                                               cover_w == 4 ~ 38,
                                               cover_w == 5 ~ 63,
                                               cover_w == 6 ~ 88))

# function to convert cover values to percent covers:
class2value <- function(cover_classes){
  cover_value = case_when(cover_classes == 1 ~ 0.5,
                          cover_classes == 2 ~ 10.5,
                          cover_classes == 3 ~ 20.5,
                          cover_classes == 4 ~ 38,
                          cover_classes == 5 ~ 63,
                          cover_classes == 6 ~ 88,
                          TRUE ~ cover_classes)
  return(cover_value)
}

class2value(cover_classes = c(2,3,5,1,0))

covers_converted <- mutate(island_cover_data,
                           cover_mid = class2value(cover_mid),
                           cover_n = class2value(cover_n),
                           cover_s = class2value(cover_s),
                           cover_e = class2value(cover_e),
                           cover_w = class2value(cover_w))

covers_converted <- mutate(island_cover_data,
                           across(cover_mid:cover_w, class2value))

covers_converted <- mutate(island_cover_data, across(contains("cover_"), class2value))





###################################
##### SUMMARY
# - Changing the value of variables using the case_when function
# - applying that function efficiently to several columns at once

