我想将每小时法国时间的字符序列转换为POSIXct。
此序列包括在02:00 (CEST => CET)发生的dst时钟更改,这会导致重复的输出时间。
char_dates <- c("2017-10-29 01:00:00",
"2017-10-29 02:00:00",
"2017-10-29 02:00:00",
"2017-10-29 03:00:00")
ymd_hms(char_dates, tz = "Europe/Paris")输出:
"2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CET" "2017-10-29 02:00:00 CET" "2017-10-29 03:00:00 CET" 所需输出(请注意第二个时区):
"2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CEST" "2017-10-29 02:00:00 CET" "2017-10-29 03:00:00 CET" 什么是实现这一目标的好解决方案?
我们可以假设日期已经排序,或者用户可以提供一个布尔向量(dst: yes/no)来告诉lubridate选择哪个时区。
发布于 2017-08-02 23:59:57
检查是否存在重复项(从末尾开始),并对重复值减去3600 (以秒为单位的1小时)。如果你有一个布尔值的向量,用它代替duplicated(temp, fromLast = TRUE)。
char_dates <- c("2017-10-29 01:00:00",
"2017-10-29 02:00:00",
"2017-10-29 02:00:00",
"2017-10-29 03:00:00")
temp = ymd_hms(char_dates, tz = "Europe/Paris")
temp[duplicated(temp, fromLast = TRUE)] = temp[duplicated(temp, fromLast = TRUE)] - 3600
temp
#[1] "2017-10-29 01:00:00 CEST" "2017-10-29 02:00:00 CEST" "2017-10-29 02:00:00 CET"
#[4] "2017-10-29 03:00:00 CET" https://stackoverflow.com/questions/45465561
复制相似问题