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