as.Date dosen't work with %d-%b-%y (R) [duplicate] - r

I often use as.POSIXct to convert characters to POSIXct, but I get NA sometimes and I don't know why. For example:
DATE <- "Fri Apr 10 11:57:47 2015"
DATE_in_posix <- as.POSIXct(DATE, format="%a %b %d %H:%M:%S %Y")
I tried this too:
DATE_in_posix <- as.POSIXct(DATE, format="%a %h %d %H:%M:%S %Y")
But result for both is always:
> DATE_in_posix
[1] NA
Maybe the input for as.POSIXct is too long? And when it's too long what could be the solution?

It's probably because "Fri" and "Apr" are not the correct abbreviations in your locale.
Use Sys.setlocale("LC_TIME", locale) to set your R session's locale to one that will correctly interpret English abbreviations. See the Examples section of ?Sys.setlocale for how to specify locale in the above function call.
For example, on my Ubuntu machine it would be:
> Sys.setlocale("LC_TIME", "en_US.UTF-8")
> as.POSIXct("Fri Apr 10 11:57:47 2015", format="%a %b %d %H:%M:%S %Y")
[1] "2015-04-10 11:57:47 CDT"

Thanks a lot Henrik!!!
I changed the LC_TIME category like this, now it works
Sys.getlocale(category = "LC_TIME")
[1] "German_Germany.1252"
Sys.setlocale("LC_TIME", "English")
[1] "English_United States.1252"
DATE_in_posix<-as.POSIXct(DATE,format="%a %b %d %H:%M:%S %Y")
> DATE_in_posix
[1] "2015-04-10 11:57:47 CEST"
and strptime now works too of course
DATE_in_posix<-strptime(DATE,format="%a %b %d %H:%M:%S %Y")
> DATE_in_posix
[1] "2015-04-10 11:57:47 CEST"
Thank you so much guys and have a nice weekend!

Related

Invalid %%A auto parser

Can anybody tell me what is wrong with this command in R?
I literally have tried everything:
d0 <- "domingo 04 febrero 2018"
parse_date(d0, "%A %d %B %Y", locale = locale("es"))
When I execute the above code, I get an error that says
"invalid %%A auto parser"
I think that the problem is that the "%A" format specification is not defined in the parse_date function.
d0 <- "04 febrero 2018"
parse_date(d0, "%d %B %Y", locale = locale("es"))
[1] "2018-02-04"
The %A is a valid format per ISO8601 specification.
The actual problem is that parse_date (a function from readr package) doesnot provide support for all of ISO8601 specifications. Missing features include Week and Weekday specifications.
But we have an alternate solution in base r itself. Let me provide you few examples:
# %A is supported is base r
> as.character(Sys.Date(), "%A %d %B %Y")
[1] "Sunday 04 February 2018"
# Try to parse character string to date now
> as.POSIXct("Sunday 04 February 2018", format = "%A %d %B %Y")
[1] "2018-02-04 GMT"
# Lets execute the same code (converted to English) from OP
d0 <- "Sunday 04 February 2018"
as.POSIXct(d0, format = "%A %d %B %Y", tz = "GMT")
# Result :
[1] "2018-02-04 GMT"

Adjust data time zone in R

For some reason I can't adjust the time zone by as.POSIXlt.
time <- "Wed Jun 22 01:53:56 +0000 2016"
t <- strptime(time, format = '%a %b %d %H:%M:%S %z %Y')
t
[1] "2016-06-21 21:53:56"
Can't change the time zone
as.POSIXlt(t, "EST")
[1] "2016-06-21 21:53:56"
as.POSIXlt(t, "Australia/Darwin")
[1] "2016-06-21 21:53:56"
Can change the time zone for Sys.time()
as.POSIXlt(Sys.time(), "EST")
[1] "2016-09-26 01:47:22 EST"
as.POSIXlt(Sys.time(), "Australia/Darwin")
[1] "2016-09-26 16:19:48 ACST"
How to solve it?
Try this:
time <- "Wed Jun 22 01:53:56 +0000 2016"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y')
#[1] "2016-06-22 07:23:56"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y', tz="EST")
#[1] "2016-06-21 20:53:56"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y', tz="Australia/Darwin")
#[1] "2016-06-22 11:23:56"
strptime returns a POSIXlt object. Calling as.POSIXlt on t just returns t. There is no as.POSIXlt.POSIXlt method, so as.POSIXlt.default is dispatched. And you can see the first if statement checks if x inherits the POSIXlt class, and returns x if that's true.
str(t)
# POSIXlt[1:1], format: "2016-06-21 20:53:56"
print(as.POSIXlt.default)
# function (x, tz = "", ...)
# {
# if (inherits(x, "POSIXlt"))
# return(x)
# if (is.logical(x) && all(is.na(x)))
# return(as.POSIXlt(as.POSIXct.default(x), tz = tz))
# stop(gettextf("do not know how to convert '%s' to class %s",
# deparse(substitute(x)), dQuote("POSIXlt")), domain = NA)
# }
# <bytecode: 0x2d6aa18>
# <environment: namespace:base>
You either need to use as.POSIXct instead of strptime and specify the timezone you want, then convert to POSIXlt:
ct <- as.POSIXct(time, tz = "Australia/Darwin", format = "%a %b %d %H:%M:%S %z %Y")
t <- as.POSIXlt(ct)
Or use strptime and convert t to POSIXct and then back to POSIXlt:
t <- strptime(time, format = "%a %b %d %H:%M:%S %z %Y")
t <- as.POSIXlt(as.POSIXct(t, tz = "Australia/Darwin"))

strptime returning NA

I am getting NA value for my date string using strptime in R.
I looked at the various answers, but it didn't work.
Here is my code
startDate=strptime("Wed May 25 01:51:32 UTC 2016", format="%a %B %d %H:%m:%S %Z %Y", tz="UTC")
print(startDate)
Any help would be appreciated.
"%H:%m:%S" should be "%H:%M:%S". Once you change that, you'll get an error because %Z is not valid for input.
If all the datetime strings have UTC timezone, this will work:
R> strptime("Wed May 25 01:51:32 UTC 2016", "%a %B %d %H:%M:%S UTC %Y", "UTC")
[1] "2016-05-25 01:51:32 UTC"
If not, then you can extract the year and prepend it to the string, because strptime will ignore all characters after those specified by the format string.
R> dts <- "Wed May 25 01:51:32 UTC 2016"
R> dtf <- "%Y %a %B %d %H:%M:%S"
R> strptime(paste(substring(dts, nchar(dts)-3), dts), dtf, "UTC")
[1] "2016-05-25 01:51:32 UTC"

Conversion from character to date/time returns NA

I often use as.POSIXct to convert characters to POSIXct, but I get NA sometimes and I don't know why. For example:
DATE <- "Fri Apr 10 11:57:47 2015"
DATE_in_posix <- as.POSIXct(DATE, format="%a %b %d %H:%M:%S %Y")
I tried this too:
DATE_in_posix <- as.POSIXct(DATE, format="%a %h %d %H:%M:%S %Y")
But result for both is always:
> DATE_in_posix
[1] NA
Maybe the input for as.POSIXct is too long? And when it's too long what could be the solution?
It's probably because "Fri" and "Apr" are not the correct abbreviations in your locale.
Use Sys.setlocale("LC_TIME", locale) to set your R session's locale to one that will correctly interpret English abbreviations. See the Examples section of ?Sys.setlocale for how to specify locale in the above function call.
For example, on my Ubuntu machine it would be:
> Sys.setlocale("LC_TIME", "en_US.UTF-8")
> as.POSIXct("Fri Apr 10 11:57:47 2015", format="%a %b %d %H:%M:%S %Y")
[1] "2015-04-10 11:57:47 CDT"
Thanks a lot Henrik!!!
I changed the LC_TIME category like this, now it works
Sys.getlocale(category = "LC_TIME")
[1] "German_Germany.1252"
Sys.setlocale("LC_TIME", "English")
[1] "English_United States.1252"
DATE_in_posix<-as.POSIXct(DATE,format="%a %b %d %H:%M:%S %Y")
> DATE_in_posix
[1] "2015-04-10 11:57:47 CEST"
and strptime now works too of course
DATE_in_posix<-strptime(DATE,format="%a %b %d %H:%M:%S %Y")
> DATE_in_posix
[1] "2015-04-10 11:57:47 CEST"
Thank you so much guys and have a nice weekend!

How can I parse date in another language?

My local environment is French :
> Sys.getlocale()
[1] "fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8"
I would like to parse date in English but I don't know how to enter this parameter to my function.
If the date is in French, everything works :
> as.Date("15 mai 2004", "%d %B %Y")
[1] "2004-05-15"
If I have a date in English, it doesn't work :
> as.Date("15 mai 2004", "%d %B %Y")
[1] "2004-05-15"
as.Date("15 may 2004", "%d %B %Y")
[1] NA
Ok, here is the solution :
Sys.setlocale(category = "LC_TIME", locale = "en_GB.UTF-8")
as.Date("15 may 2004", "%d %B %Y")

Resources