data-raw/treatments/code_treatments.R

###############################################################################
# Author: Gina, PhD student w/Sotiris (vnichols@iastate.edu)
#         Pat, field coordinator w/Sotiris
#
# Date created: May 18 2020
#
# Purpose: take user-interface excel sheet from Javed and create a tidy version
#
# Inputs: soilN2018, soilN2019
#
# Outputs:
#
# NOtes: Not sure what should be included. Compound columns better than separated?
#
#
# Date last modified:
###############################################################################

library(usethis)
library(dplyr)
library(readr)
library(readxl)
library(lubridate)
library(purrr)
library(tidyr)
library(stringr)

# 2018 --------------------------------------------------------------------

raw18 <- read_excel("data-raw/treatments/soilN2018.xlsx", sheet = "Treatments",
           na = c("#N/A", "#VALUE", "."))


#--create a unique plot id
trts18 <-
  raw18 %>%
  rename(site = Location,
         trt_name = trt,
         crop_type = crop) %>%
  mutate(year = 2018,
         site_name = toupper(site),
         site_id = str_sub(site, 1, 4),
         trt_id = paste(crop_type, trt_name, sep = "_")) %>%
    mutate(plot = paste0("P", toupper(plot)),
           plot_id = paste(year, site_id, plot, sep = "_")) %>%
    select(plot_id, trt_id, site)



# NOTE: not sure this is necessary

# Get the number of unique treatments in each year/site/crop
  # Relabel treatments as 1, 2, 3, etc... w/in each year/site/crop
  # Note, using rank is the best way to do this.
  #
  # arrange(year, site, crop, trt_name, rep) %>%
  # group_by(year, site, crop) %>%
  # mutate(trt_id = dense_rank(trt_name)) %>%
  # select(year, site, plot, crop, trt_name, rep, trt_id) %>%
  # ungroup() %>%
  # mutate(site = str_to_upper(site))



# 2019 --------------------------------------------------------------------

raw19 <- read_excel("data-raw/treatments/soilN2019.xlsx", sheet = "Treatments",
                    na = c("#N/A", "#VALUE", "."))


#--create a unique plot id
#raw_trts <-
  raw19 %>%
  mutate(year = 2019,
         plot_id = paste0(year, plotID),
         plot = paste0("P-", toupper(plot)),
         Location = toupper(Location)) %>%
  select(-plotID) %>%
  rename(site = Location,
         trt_name = trt,
         crop_type = crop) %>%
  select(plot_id, crop_type, trt_name)



# write it ----------------------------------------------------------------

facts_inventory <- tidyinv

facts_inventory %>%
  write_csv("data-raw/inventory/facts_inventory.csv")


use_data(facts_inventory, overwrite = T)
vanichols/JustTheFACTS documentation built on May 24, 2020, 5:31 a.m.