I have a dataframe with dates and I am trying to convert it to a POSIXct object but I am unable to specify timezone. Any idea why this is happening?
> str(dates)
'data.frame': 3171 obs. of 3 variables:
$ Date : Date, format: "2013-05-14" "2013-08-15" "2014-05-30" "2014-09-29" ...
$ BB_Ticker: Factor w/ 1252 levels "A US Equity",..: 1 2 2 2 2 2 2 2 2 2 ...
$ 1Y : POSIXct, format: "2013-05-13 20:00:00" "2013-08-14 20:00:00" "2014-05-29 20:00:00" "2014-09-28 20:00:00" ..
I tried specifying "America/New_York"
as well as "EST5EDT"
but it had no effect -
> head(as.POSIXct(dates$Date, tz = "GMT"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"
> head(as.POSIXct(dates$Date, tz = "America/New_York"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"
> head(as.POSIXct(dates$Date, tz = "EST5EDT"), 3)
[1] "2013-05-13 20:00:00 EDT" "2013-08-14 20:00:00 EDT" "2014-05-29 20:00:00 EDT"
There are two POSIX date/time classes, which differ in the way that the values are stored internally. The POSIXct class stores date/time values as the number of seconds since January 1, 1970, while the POSIXlt class stores them as a list with elements for second, minute, hour, day, month, and year, among others.
POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.
The basic POSIX measure of time, calendar time, is the number of seconds since the beginning of 1970, in the UTC timezone (GMT as described by the French).
If you look at the source code of as.POSIXct.Date
you see this:
function (x, ...)
.POSIXct(unclass(x) * 86400)
<bytecode: 0x00000000120de6e0>
<environment: namespace:base>
Note how no timezone is passed on to .POSIXct
.
You can use the character method instead:
as.POSIXct(as.character(as.Date("2013-05-14")), tz = "GMT")
[1] "2013-05-14 GMT"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With