1

I need to transform dates in R which have the format e.g. "01OCT2011". The Problem is that the function as.Date only considers German months. Here, I have an example:

> test <- c("15MAI2006","01OCT2011")
> test1 <- as.Date(test, format='%d%B%Y')
> test1
[1] "2006-05-15" NA 

"MAY" is in German "MAI". The function didn't get the date with the English spelling OCT.

bathyscapher
  • 1,615
  • 1
  • 13
  • 18
I.AH
  • 27
  • 1
  • 7

2 Answers2

2

If you really want to get both English and German dates, you will need to get them one at a time.

Sys.setlocale("LC_TIME", "de_DE")
test1 <- as.Date(test, format='%d%B%Y')
na.test1 <- is.na(test1)
Sys.setlocale("LC_TIME", "C")
test1[na.test1] <- as.Date(test[na.test1], format='%d%B%Y')

The locale I used for German up above is for OSX, but you can find the formats for other systems at documentation for Sys.setlocale

waternova
  • 1,472
  • 3
  • 16
  • 22
1

If you are 'completely sure' that the language is German try this:

Sys.setlocale("LC_ALL","German")

test <- c("15MAI2006","01OKT2011")
as.Date(test, format='%d%B%Y')

[1] "2006-05-15" "2011-10-01"

However, you have in your original data "01OCT2011" and October should be Okt

Edgar Santos
  • 3,426
  • 2
  • 17
  • 29
  • Don't think so. This produce the same results: as.Date(c("01Okt2011","01OKT2011", "01okt2011") , format='%d%B%Y') – Edgar Santos Jun 20 '17 at 21:44
  • No ist vice versa. My Dates are in english and I want to transform them also in english. But I think in my case R just consider german month and not the english. Thats why my Output is confirm with "MAI" but not with"MAY". Same with "OCT". I have tried Sys.setlocale("LC_ALL","English")... but doesnt work. – I.AH Jun 20 '17 at 21:47
  • That would be logical yes. Something's wrong with my system maybe I can't get the same results – Paul Endymion Jun 20 '17 at 21:51
  • Try sessionInfo() and make sure that your language is English. – Edgar Santos Jun 20 '17 at 21:51
  • @I.AH try `Sys.setlocale("LC_TIME", "C")` – azalea Jun 20 '17 at 21:54