###################################################################
############### Module 6, Basics of data wrangling ################
###################################################################
## (Bonus) Lesson 14, Part 1: Dealing with dates (parsing and date math)

# Load the tidyverse packages:
library(tidyverse)

###################################
##### LOAD THE DATA
# For this lesson we will use data on the date of first flowering of 
# plants in the Northern Great Plains of North America:
# Steven E. Travers and Kelsey L. Dunnell. 2009. 
# First-flowering dates of plants in the Northern Great Plains. Ecology 90:2332.
# http://www.esapubs.org/archive/ecol/E090/162/default.htm#data
# The data are available between the years of 1910 and 1961 and essentially
# consist of the date each year when each species first flowered after the winter.

ff_data_raw <- read_csv("data/first_flower_data.csv")


###################################
##### PREPARE THE DATA

ff_data_clean <- pivot_longer(ff_data_raw, cols = `1910`:`1961`, names_to = "year")

species <- paste(ff_data_raw$Genus, ff_data_raw$Species)

length(unique(species))
# 52 unique years

52*743

nrow(ff_data_clean)

species[duplicated(species)]

ff_data_nodupes <- distinct(ff_data_clean, Genus, Species, year, .keep_all=T)

ff_data_clean_fin <- select(ff_data_nodupes, -`Common Names`, -Family, -Synonym, -Site) %>% 
  mutate(species = paste(Genus, Species)) %>% 
  select(-Genus, -Species, date=value) %>% 
  mutate(date = ifelse(date == ".", NA, date))

###################################
##### PARSING DATES AND DATE MATH

# To get a bunch of functions that will help us work with dates
# more easily, install and load the "lubridate" package
# install.packages("lubridate")
library("lubridate")


"07/21/1957"

parsed_date <- mdy("march 5th, 2010")
dmy("5/3/2010")

ff_data_final <- mutate(ff_data_clean_fin, date = mdy(date))


parsed_date - 10

parsed_date - years(1)
parsed_date - months(1)
parsed_date - weeks(1)

ff_data_final <- mutate(ff_data_clean_fin, date = mdy(date) - years(100))

yday(parsed_date)

month(parsed_date, label=T)
ff_data_final <- mutate(ff_data_clean_fin, date = mdy(date) - years(100),
                        julianday = yday(date),
                        month = month(date, label=T),
                        year = as.numeric(year))


ff_data_summ <- filter(ff_data_final, !is.na(date)) %>% 
  group_by(species) %>% 
  summarize(day_mean = mean(julianday, na.rm=T),
            day_earliest = min(julianday, na.rm=T),
            day_latest = max(julianday, na.rm=T),
            sample_N = n()) %>% 
  ungroup() %>% 
  filter(sample_N >= 35) %>% 
  mutate(day_mean_d = dmy("01/01/1900") + day_mean,
         earliest_d = dmy("01/01/1900") + day_earliest,
         latest_d = dmy("01/01/1900") + day_latest)


###################################
##### SUMMARY
# - Reviewed some data cleaning procedures and pivot_longer
# - Parsing dates
# - Date math
# - Summarizing dates

