# tree ring data library(mccrr) library(lubridate) library(tidyverse) library(jsonlite) library(tibble) library(ggplot2) library(dplyr) library(tidyr) pdsi <- readr::read_csv("C:\\Users\\Andrew\\Desktop\\Statistics and Data Analysis\\climate_data\\NADAv2a.csv") metadata <- jsonlite::fromJSON(txt = "C:\\Users\\Andrew\\Desktop\\Statistics and Data Analysis\\climate_data\\metadata\\noaa-recon-6319.json", simplifyVector = F) pdsi_L <- pdsi %>% pivot_longer(-year, names_to = "station", values_to = "pdsi", values_drop_na = TRUE) meta_raw <- enframe(unlist(metadata)) meta_clean <- meta_raw %>% # takes unlisted data, removes the several thousand variables we don't need, # creates a variable to be widened later # then recodes them by row number (dangerous! but they *are* in order) # to join them to the original pdsi data set. filter(stringr::str_detect(name, 'coordinates')) %>% mutate(latlong = case_when( grepl("1", name, fixed=TRUE) ~ "longitude", grepl("2", name, fixed=TRUE) ~ "latitude" )) %>% group_by(name) %>% mutate(station = row_number()) %>% ungroup() %>% select(-name) %>% pivot_wider(names_from = latlong, values_from = value) trees <- pdsi_L %>% mutate(station = as.numeric(station)) %>% left_join(meta_clean, by = "station")