Elvis Relaxing with Sunbathing
Sequential Filenames or Folders
After saving estimation results in an 'AA' folder, subsequent results with different configurations are stored in sequentially numbered folders such as 'AA_01', 'AA_02', and so on. Of course, this version history management also applies to files.
This method is useful to save forecasting results with varying specifications, such as different models or sample periods, for ongoing comparative analysis.
Sequential Folders
The following R code is used for creating sequential folders.
setwd('your working directory') # creates sequentially numbered folders create_numbered_folder <- function(str_nm, digits=2) { # Create the folder only if it doesn't exist if (!file.exists(str_nm)) { folder_name <- str_nm } else { kk <- 1 folder_name <- sprintf("%s_%0*d", str_nm, digits, kk) while (file.exists(folder_name)) { kk <- kk + 1 folder_name <- sprintf("%s_%0*d", str_nm, digits, kk) } } dir.create(folder_name) return(folder_name) } # Example usage: str_nm <- "AA" new_folder <- create_numbered_folder(str_nm) print(paste("Created folder:", new_folder)) | cs |
> [1] "Created folder: AA" > [1] "Created folder: AA_01" > [1] "Created folder: AA_02" > | cs |
Sequential Filenames
The following R code is used for creating sequential filenames.
# creates sequentially numbered file names create_numbered_file_name <- function(base_name, digits = 2, extension = ".csv") { # Create the folder only if it doesn't exist base_name_we <- sprintf("%s%s", base_name, extension) if (!file.exists(base_name_we)) { file_name <- base_name_we } else { kk <- 1 file_name <- sprintf("%s_%0*d%s", base_name, digits, kk, extension) while (file.exists(file_name)) { kk <- kk + 1 file_name <- sprintf("%s_%0*d%s", base_name, digits, kk, extension) } return(file_name) } } # Example usage: base_file_name <- "FF" new_file_name <- create_numbered_file_name(base_file_name) print(paste("file name:", new_file_name)) # writing row in the csv file write.table(NA, file = new_file_name, sep = ",", append = TRUE, quote = FALSE, col.names = FALSE, row.names = FALSE) | cs |
> [1] "file name: FF.csv" > [1] "file name: FF_01.csv" > [1] "file name: FF_02.csv" > | cs |
No comments:
Post a Comment