I.AH
I.AH

Reputation: 27

function as.Date just recognizes german month - but not english month

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.

Upvotes: 1

Views: 3039

Answers (2)

waternova
waternova

Reputation: 1568

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

Upvotes: 2

Edgar Santos
Edgar Santos

Reputation: 3504

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

Upvotes: 1

Related Questions