Jon Jon - 29 days ago 9
R Question

as.Date returning NA after setting C locale

I am trying to set the C locale so that when I convert character objects to dates I don't get NA values. I got this solution from this question, but it's not working for me. When I run the first commented line,

> lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")


I get the following warning:

Warning message:
In readLines(outfile) :
incomplete final line found on 'tmp/Rtmp8AJy9P/Rhttpd9742ffa74dc'
[1] "C"


Then when I try to convert a character object to date, I still get an NA of class date.

> x <- "2015-02-15"
> z <- as.Date(x, "%Y-%B-%d")
> Sys.Setlocale("LC_TIME", lct)
[1] "en_US.UTF-8"
> z
[1] NA


Does anyone know what that warning means and if it could be affecting the conversion that I run later?

Answer

In

> x <- "2015-02-15"
> z <- as.Date(x, "%Y-%B-%d")
> Sys.Setlocale("LC_TIME", lct)
[1] "en_US.UTF-8"
> z
[1] NA

you have the wrong format as %B stands for the textual, unabbreviated month. Witness:

R> as.Date("2015-02-15", "%Y-%m-%d")
[1] "2015-02-15"
R> as.Date("2015-February-15", "%Y-%B-%d")
[1] "2015-02-15"
R> 

so I think your locale issue may be a distraction.