Hello, I'm trying to run the code below, which I think is supposed to take a group of 20 csv files and string them together into a single csv. The filenames of the csvs combine one of five scenarios (USG1, USG2, USG3, USG4, and USG5) and separated by a space, one of four discount rates (0.0, 0.01, 0.02, and 0.03) for a total of 20 csvs. The data in each csv records the presence or absence of discontinuities in each of 10,000 monte carlo simulations for each of 9 different years. I put the 20 csv files on github (GitHub - chframe/HFC-social-costs: discontinuities in PAGE integrate assessment model runs). The problem is that the final csv that I get at the end does not have the scenarios (USG1, USG2, etc) recorded in the scenario column of the data frame. Instead the scenario column just has 'NA' all the way down for 1,800,000 rows. I believe everything else came out fine in the final csv
It looks like the code is trying to convert the USG scenarios into more specific names (e.g. "IMAGE", "MERGE Optimistic", etc). I don't need it to do this. But I do need to record the original scenario names in the combined csv. Does anyone have any ideas for why it's doing this? Thanks for your help. Please let me know if there's something I can do to clarify what's happening. Thanks
## Written by: US EPA National Center for Environmental Economics
##########################
################# LIBRARY
##########################
## Clear worksace
rm(list = ls())
gc()
## This function will check if a package is installed, and if not, install it
list.of.packages <- c('tidyverse','magrittr','stringr')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, repos = "http://cran.rstudio.com/")
lapply(list.of.packages, library, character.only = TRUE)
library(data.table)
##########################
################ PREAMBLE
##########################
## List of gases
hfc_list <- c('125','134a','143a')
##########################
############## START LOOP
##########################
for (hfc in hfc_list) {
page_dir <- paste0("/home/julia-1.6.3/share/julia/MimiIWG-HFC/data/hfc",hfc,"/page/discontinuity_mismatch") # location of file group
page_files <- fs::dir_ls(page_dir, regexp = "\\.csv$") # create list of .csv files
page <- page_files %>%
map_dfr(read_csv, .id = "source") %>% # read in files (map), turn into data frame (df), and row bind (r)
as.data.table()
##########################
################### CLEAN
##########################
page %<>% mutate(source = str_remove(source,paste0("data/hfc",hfc,"/page/discontinuity_mismatch/"))) %>%
mutate(source = str_remove(source,".csv")) %>%
separate(source, c("scenario","discount_rate"), " ") %>%
mutate(scenario = case_when(scenario=="USG1" ~ "IMAGE",
scenario=="USG2" ~ "MERGE Optimistic",
scenario=="USG3" ~ "MESSAGE",
scenario=="USG4" ~ "MiniCAM Base",
scenario=="USG5" ~ "5th Scenario"),
discount_rate = paste0(as.numeric(discount_rate)*100,'%')) %>%
group_by(scenario,discount_rate) %>%
mutate(trial = seq(n()))
## WIDE TO LONG
years <- paste(seq(2020,2060,5), sep=", ") # vector of years
page %<>% gather(year,discontinuity,all_of(years)) %>%
mutate(model = 'PAGE 2009')
##########################
#################### SAVE
##########################
write_csv(page, paste0("/home/julia-1.6.3/share/julia/MimiIWG-HFC/data/hfc",hfc,"_page_discontinuity.csv"))
}
## END OF SCRIPT. Have a great day!