as.POSIXct 的时区无效
Timezone for as.POSIXct not working
我有一个带日期的数据框,我正在尝试将其转换为 POSIXct 对象,但我无法指定时区。知道为什么会这样吗?
> 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" ..
我尝试指定 "America/New_York"
以及 "EST5EDT"
但没有效果 -
> 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"
如果您查看 as.POSIXct.Date
的源代码,您会看到:
function (x, ...)
.POSIXct(unclass(x) * 86400)
<bytecode: 0x00000000120de6e0>
<environment: namespace:base>
请注意没有时区传递给 .POSIXct
。
您可以改用字符法:
as.POSIXct(as.character(as.Date("2013-05-14")), tz = "GMT")
[1] "2013-05-14 GMT"
我有一个带日期的数据框,我正在尝试将其转换为 POSIXct 对象,但我无法指定时区。知道为什么会这样吗?
> 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" ..
我尝试指定 "America/New_York"
以及 "EST5EDT"
但没有效果 -
> 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"
如果您查看 as.POSIXct.Date
的源代码,您会看到:
function (x, ...)
.POSIXct(unclass(x) * 86400)
<bytecode: 0x00000000120de6e0>
<environment: namespace:base>
请注意没有时区传递给 .POSIXct
。
您可以改用字符法:
as.POSIXct(as.character(as.Date("2013-05-14")), tz = "GMT")
[1] "2013-05-14 GMT"