Convert char to POSIXct - r

this is my date to convert:
"16:00:00 CT 08 Apr 2018"
and this is my try:
x <- "16:00:00 CT 08 Apr 2018"
Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")
as.POSIXct(x, format = '%H:%M:%S %A %d %b %Y')
and it returns NA

We can use the CT as such in the format
as.POSIXct(x, format = '%H:%M:%S CT %d %b %Y')

Remove string CT and go further:
as.POSIXct(paste(unlist(strsplit(x," CT ")),collapse = ""),format='%H:%M:%S %d %b %Y')
[1] "2018-04-08 16:00:00 GMT"

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"))

R converting twitter created_at string to time

I am trying to convert the created_at string but it returns NA
as.POSIXct("Tue Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] NA
Any idea what's going wrong, seems fairly straightforward!
Conversion of dates depends on your locale. For me, this is Slovene, so your case doesn't work.
> as.POSIXct("Tue Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] NA
However, if I change the date to Slovene (Tor = torek = Tuesday)
> as.POSIXct("Tor Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] "2016-06-07 23:27:12 GMT"
In short, change your locale to English and you're set.
> Sys.setlocale("LC_TIME", "English")
[1] "English_United States.1252"
> as.POSIXct("Tue Jun 07 23:27:12 +0000 2016", format="%a %b %d %H:%M:%S +0000 %Y", tz="GMT")
[1] "2016-06-07 23:27:12 GMT"
a solution that doesn't involve changing your locale
library(dplyr)
library(magrittr)
twitter_to_POSIXct <- function(x, timezone = Sys.timezone()){
x %>%
strsplit("\\s+") %>%
unlist %>%
t %>%
as.data.frame(stringsAsFactors = FALSE) %>%
set_colnames(c("week_day", "month_abb",
"day", "hour", "tz",
"year")) %>%
mutate(month_num = which(month.abb %in% month_abb)) %>%
mutate(date_str = paste0(year, "-", month_num, "-", day, " ",
hour)) %>%
mutate(date = format(as.POSIXct(date_str, tz = tz),
tz = timezone)) %>%
pull(date)
}
twitter_to_POSIXct("Tue Jun 07 23:27:12 +0000 2016")

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"

Converting time format in R

One of my observation of time data is "Wed Oct 31 13:42:46 2007".
I want to convert to the format as "2007-10-31 13:42:46" (remove Wed).
How I can convert it?
As #thelatemail said
x <- "Wed Oct 31 13:42:46 2007"
x <- as.POSIXct(x, format = "%a %b %d %T %Y", tz = "GMT")
# %a day of week, %b month of year, %d day of month as decimal, %T H:M:S, %Y year with century
x
[1] "2007-10-31 13:42:46 GMT"

Resources