Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change german month abbreviation in full name of the month in R

Tags:

r

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

like image 224
バシル Avatar asked Sep 12 '25 01:09

バシル


2 Answers

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" 
like image 108
GKi Avatar answered Sep 14 '25 19:09

GKi


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.

like image 20
Konrad Rudolph Avatar answered Sep 14 '25 18:09

Konrad Rudolph