来自date squence的时区POSIXct在强制转换为R中的data.frame时丢失

时间:2018-05-10 10:45:04

标签: r timezone posixct

我想保留下面生成的数据序列的CET,CEST部分。

seq(as.POSIXct("2018-10-01"), as.POSIXct("2018-10-02"), "hour")
myvector <- seq(as.POSIXct("2018-10-01"), as.POSIXct("2018-10-02"), "hour")
myvector
mydf <- as.data.frame(myvector)

在控制台中看起来像:

> head(seq(...))

[1] "2018-10-01 00:00:00 CEST" "2018-10-01 01:00:00 CEST" "2018-10-01 02:00:00 CEST" "2018-10-01 03:00:00 CEST" "2018-10-01 04:00:00 CEST" "2018-10-01 05:00:00 CEST"

> head(myvector)

[1] "2018-10-01 00:00:00 CEST" "2018-10-01 01:00:00 CEST" "2018-10-01 02:00:00 CEST" "2018-10-01 03:00:00 CEST" "2018-10-01 04:00:00 CEST" "2018-10-01 05:00:00 CEST"

> head(mydf)
             myvector
1 2018-10-01 00:00:00
2 2018-10-01 01:00:00
3 2018-10-01 02:00:00
4 2018-10-01 03:00:00
5 2018-10-01 04:00:00
6 2018-10-01 05:00:00
> 

当我将它强制转换为data.frame时,它会丢失。我不知道如何保留它,我尝试过这样的事情:

attr(mydf$myvector, "tzone") <- attr(myvector, "tzone")tzone不是真正的属性,所以它不起作用。

CEST/CET中的POSIXct是什么?在胁迫到df时如何保留它?

由于

1 个答案:

答案 0 :(得分:1)

您需要在BDD列上应用as.POSIXlt才能从中获取时区

POSIXct

示例数据:

#Extract timezone from POSIXct column of a dataframe
mydf$timezone <- attr(as.POSIXlt(mydf$myvector), "tzone")[1]

head(mydf)
#             myvector      timezone
#1 2018-10-01 00:00:00 Europe/Berlin
#2 2018-10-01 01:00:00 Europe/Berlin
#3 2018-10-01 02:00:00 Europe/Berlin
#4 2018-10-01 03:00:00 Europe/Berlin
#5 2018-10-01 04:00:00 Europe/Berlin
#6 2018-10-01 05:00:00 Europe/Berlin


替代方法:如果您真的关心myvector <- seq(as.POSIXct("2018-10-01"), as.POSIXct("2018-10-02"), "hour") head(myvector) #[1] "2018-10-01 00:00:00 CEST" "2018-10-01 01:00:00 CEST" "2018-10-01 02:00:00 CEST" #[4] "2018-10-01 03:00:00 CEST" "2018-10-01 04:00:00 CEST" "2018-10-01 05:00:00 CEST" mydf <- as.data.frame(myvector) head(mydf$myvector) #[1] "2018-10-01 00:00:00 CEST" "2018-10-01 01:00:00 CEST" "2018-10-01 02:00:00 CEST" #[4] "2018-10-01 03:00:00 CEST" "2018-10-01 04:00:00 CEST" "2018-10-01 05:00:00 CEST" CET仅输出

CEST