I want to create a function for detecting the month names from list of filenames and replace the month names with a numeric value. Can we use a dictionary for creating such function and apply to the list.
f <- c("cm18OCT2023.csv", "cm22NOV2023.csv")
month_text <- c("JAN" = "01",
"FEB" = "02",
"MAR" = "03",
"APR" = "04",
"MAY" = "05",
"JUN" = "06",
"JUL" = "07",
"AUG" = "08",
"SEP" = "09",
"OCT" = "10",
"NOV" = "11",
"DEC" = "12")
newnames <- ifelse(str_detect(f, names(month_text)), new_names, f)
The final output can be of new names such as paste0("23", value(month_text), str_sub(3,4),".csv") for a new list.
You could have this cheaper using the built-in
month.abb.Data: