Reputation: 45
I would like to change german month abbreviations like "Jan", "Feb", "Mär, "Apr", "Mai", and so on, to the full name of the months. Like "Januar", "Februar", "März", ...
I guess there is no such function available. For English abbreviations, there is the function month.abb
. Do you have an idea how I could do this not manually?
Thanks a lot
Upvotes: 4
Views: 1231
Reputation: 39727
You can get full month name in the current locale with:
format(ISOdate(2000, 1:12, 1), "%B")
#months(ISOdate(2000, 1:12, 1)) #Alternative
and abbreviated month name in the current locale with:
format(ISOdate(2000, 1:12, 1), "%b")
#months(ISOdate(2000, 1:12, 1), TRUE) #Alternative
To change the locale have a look at How to change the locale of R? in case Sys.setlocale("LC_TIME", "de_DE.UTF-8")
is not working.
And to make the conversion local short to local long:
loc2loc <- setNames(format(ISOdate(2000, 1:12, 1), "%B"), format(ISOdate(2000, 1:12, 1), "%b"))
loc2loc["Jan"]
and local short to English long:
loc2eng <- setNames(month.name, format(ISOdate(2000, 1:12, 1), "%b"))
loc2eng["Jan"]
Or without using locales - hard coded:
de2de <- setNames(c("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli"
, "August", "September", "Oktober", "November", "Dezember"), c("Jan", "Feb"
, "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"))
de2de[c("Jan", "Feb", "Mär", "Apr", "Mai")]
# Jan Feb Mär Apr Mai
# "Januar" "Februar" "März" "April" "Mai"
Upvotes: 6
Reputation: 1959
When I needed to do this, I wrote a function call. Useful for vector operations and for coping with different possible inputs:
month.de <- function(mnth = "Jan") {
dplyr::case_when(
mnth == "Jan" ~ "Januar",
mnth == "Feb" ~ "Febuar",
mnth %in% c("Mar", "Mär") ~ "März",
mnth == "Apr" ~ "April",
mnth == "Mai" ~ "Mai",
mnth == "Jun" ~ "Juni",
mnth == "Jul" ~ "Juli",
mnth == "Aug" ~ "August",
mnth == "Sep" ~ "September",
mnth == "Okt" ~ "Oktober",
mnth == "Nov" ~ "November",
mnth == "Dez" ~ "Dezember",
mnth == TRUE ~ "Unknown")
}
Then month.de("Mär")
gives "März".
Upvotes: 2
Reputation: 546093
This is what the function match
solves.
Using the built-in English names, you’d write
example_data = c('Jan', 'Dec', 'Mar')
month.name[match(example_data, month.abb)]
The same works for other languages, you’ll just have to define your own vectors for the month names and abbreviations.
Upvotes: 5