###################################################################
############### Module 6, Basics of data wrangling ################
###################################################################
## Lesson 13, Part 3: Reshaping your data and some other useful functions (implicitly missing values)


# First load the tidyverse packages and then 
# upload the saved dataframe from the last part:
library(tidyverse)
covers_data_all <- read_csv("data/covers_data_all.csv")

###################################
##### FILLING IN IMPLICITLY MISSING VALUES

b_halimifolia <- filter(covers_data_all, species == "baccharis halimifolia")

plot(understory ~ clay, data=b_halimifolia)

length(unique(covers_data_all$species))

20*93

nrow(covers_data_all)

# complete(), and expand()

covers_data_summ <- read_csv("data/covers_data_summ.csv")

covers_data_comp <- complete(covers_data_summ, expand(covers_data_summ, island, species),
                             fill=list(understory=0, midstory=0))

island_soils <- read_csv("data/island_soil_data_raw.csv")

b_halimifolia <- filter(covers_data_comp, species == "baccharis halimifolia") %>% 
  left_join(island_soils)

plot(understory ~ Clay_perc, data=b_halimifolia)


###################################
##### SUMMARY
# - How to fill in implicitely missing data using the 
#   complete and expand functions.


