Know the month abbreviations [🇷 for BE/BA]

posted by Helmut Homepage – Vienna, Austria, 2025-11-13 13:45 (216 d 11:43 ago) – Posting: # 24496
Views: 2,445

Hi ElMaestro,

❝ Well, some degree of sanity is part of my general goals. But obviously, in that case I should not have gone into bioequivalence to start with.


:-D

❝ […] a csv file with dates like "03/Apr/2024" …

❝ The solution I see is to translate every month abbrev in the data to the corresponding month abbrev of my locale and only then can it be sorted. Same thing if I convert the month abbrev to integer. Both options are equally viable. Clumsy, but I will get there. :-)


Do you import the data with read.csv()? I would suggest read.table() instead. Then you can makes sure the ugly column is read as characters and it’s easy to split it into day, month, year, and convert to an ISO-date.

months <- function(lang = "en") {
  if (!lang %in% c("en", "at", "de", "da", "fr"))
    stop ("language \"", lang, "\" not implemented.")
  switch(lang,
    "en" = setNames(1:12, month.abb),
    "at" = setNames(1:12, c("Jän", "Feb", "Mär", "Apr", "Mai", "Jun",
                            "Jul", "Aug", "Sep", "Okt", "Nov", "Dec")),
    "de" = setNames(1:12, c("Jan", "Feb", "Mrz", "Apr", "Mai", "Jun",
                            "Jul", "Aug", "Sep", "Okt", "Nov", "Dec")),
    "da" = setNames(1:12, c("jan", "feb", "mar", "apr", "maj", "jun",
                            "jul", "aug", "sep", "okt", "nov", "dec")),
    "fr" = setNames(1:12, c("janv.", "févr.", "mars", "avr.", "mau", "juin",
                            "juil.", "août", "sept.", "oct.", "nov.", "déc.")))
}
iso.date <- function(lang = "en", datum, sep = "/") {
  if (!lang %in% c("en", "at", "de", "da", "fr"))
    stop ("language \"", lang, "\" not implemented.")
  sep.locs <- gregexec(sep, datum, perl = TRUE)[[1]][1, ]
  if (!length(sep.locs) == 2)
    stop ("sep \"", sep, "\" must occur exactly twice in each date.")
  d <- substr(datum, 1, sep.locs[1] - 1)
  m <- substr(datum, sep.locs[1] + 1, sep.locs[2] - 1)
  m <- sprintf("%02d", which(names(months(lang)) == m))
  y <- substr(datum, sep.locs[2] + 1, nchar(datum))
  paste0(y, "-", m, "-", d)
}
# Example with random dates
set.seed(123456)
days    <- 7
mons    <- 6
day     <- sprintf("%02d", round(runif(days, 1, 28), 0))
month   <- round(runif(mons, 1, 12))
en.date <- paste0(day, "/", names(months("en")[month]), "/", "2025")
de.date <- paste0(day, "/", names(months("de")[month]), "/", "2025")
da.date <- paste0(day, "/", names(months("da")[month]), "/", "2025")
fr.date <- paste0(day, "/", names(months("fr")[month]), "/", "2025")
dates   <- data.frame(lang = rep(c("en", "de", "da", "fr"), each = days),
                      date = c(en.date, de.date, da.date, fr.date), iso = NA)
for (j in 1:nrow(dates)) {
 dates$iso[j] <- iso.date(lang = dates$lang[j], dates$date[j])
}
dates$iso <- as.Date(dates$iso, format = "%F")
str(dates); print(dates[with(dates, order(lang, iso)), ], row.names = FALSE, right = FALSE)

'data.frame':   28 obs. of  3 variables:
 $ lang: chr  "en" "en" "en" "en" ...
 $ date: chr  "23/Feb/2025" "21/Dec/2025" "12/Mar/2025" "10/Oct/2025" ...
 $ iso : Date, format: "2025-02-23" "2025-12-21" ...
 lang date          iso       
 da   15/feb/2025   2025-02-15
 da   23/feb/2025   2025-02-23
 da   12/mar/2025   2025-03-12
 da   11/aug/2025   2025-08-11
 da   10/okt/2025   2025-10-10
 da   06/nov/2025   2025-11-06
 da   21/dec/2025   2025-12-21
 de   15/Feb/2025   2025-02-15
 de   23/Feb/2025   2025-02-23
 de   12/Mrz/2025   2025-03-12
 de   11/Aug/2025   2025-08-11
 de   10/Okt/2025   2025-10-10
 de   06/Nov/2025   2025-11-06
 de   21/Dec/2025   2025-12-21
 en   15/Feb/2025   2025-02-15
 en   23/Feb/2025   2025-02-23
 en   12/Mar/2025   2025-03-12
 en   11/Aug/2025   2025-08-11
 en   10/Oct/2025   2025-10-10
 en   06/Nov/2025   2025-11-06
 en   21/Dec/2025   2025-12-21
 fr   15/févr./2025 2025-02-15
 fr   23/févr./2025 2025-02-23
 fr   12/mars/2025  2025-03-12
 fr   11/août/2025  2025-08-11
 fr   10/oct./2025  2025-10-10
 fr   06/nov./2025  2025-11-06
 fr   21/déc./2025  2025-12-21

Dif-tor heh smusma 🖖🏼 Довге життя Україна! [image]
Helmut Schütz
[image]

The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes

Complete thread:

UA Flag
Activity
 Admin contact
23,654 posts in 4,992 threads, 1,571 registered users;
164 visitors (0 registered, 164 guests [including 11 identified bots]).
Forum time: 02:29 CEST (Europe/Vienna)

“Data! Data! Data!” he cried impatiently.
“I can’t make bricks without clay!”    Arthur Conan Doyle

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5