lubridate 的奇怪行为:将特定天数添加到特定日期会产生 NA
Weird behavior with lubridate: adding a specific number of days to a specific date produces NA
我正在使用 lubridate
对日期执行多项操作,有时我需要添加 6 天到我正在处理的日期。我在一个循环中这样做,它几乎适用于每个日期,但在这个特定的日期上,总和 returns NA
library(lubridate)
testDate <- ymd("2018-10-29", tz = "America/Sao_Paulo")
testDate + days(4) #OK
testDate + days(5) #OK
testDate + days(6) #Returns NA
testDate + days(7) #OK
testDate + days(8) #OK
testDate + days(9) #OK
谁能帮我理解为什么会这样?
Session 信息:
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.7.4
loaded via a namespace (and not attached):
[1] compiler_3.6.0 magrittr_1.5 tools_3.6.0 Rcpp_1.0.3 stringi_1.4.3 stringr_1.4.0
正如@akrun 所建议的,这是夏令时的问题。查看6
前后的日子:
format(testDate + days(c(5,7)), format = "%Y-%m-%d %H:%M:%S")
# [1] "2018-11-03 00:00:00" "2018-11-05 00:00:00"
您会注意到它假定 "midnight"(正如人们所期望的那样)。
2018年巴西圣保罗,4号(ref)半夜前滚:
When local standard time was about to reach
Sunday, November 4, 2018, 12:00:00 midnight clocks were turned forward 1 hour to
Sunday, November 4, 2018, 1:00:00 am local daylight time instead.
也就是说,11 月 3 日,23:59:59 存在,但它一向前一秒,就变成了 11 月 4 日,01:00:00。这表明(在数学上)11 月 3 日 00:00:00 从未存在过。
因此,R 通过返回 NA
.
告诉您 timestamp 不是合法时间
但是,您可以很容易地处理 Date
个对象:
testDate + days(5:7)
# [1] "2018-11-03 -03" NA "2018-11-05 -02"
as.Date(testDate) + days(5:7)
# [1] "2018-11-03" "2018-11-04" "2018-11-05"
我正在使用 lubridate
对日期执行多项操作,有时我需要添加 6 天到我正在处理的日期。我在一个循环中这样做,它几乎适用于每个日期,但在这个特定的日期上,总和 returns NA
library(lubridate)
testDate <- ymd("2018-10-29", tz = "America/Sao_Paulo")
testDate + days(4) #OK
testDate + days(5) #OK
testDate + days(6) #Returns NA
testDate + days(7) #OK
testDate + days(8) #OK
testDate + days(9) #OK
谁能帮我理解为什么会这样?
Session 信息:
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.7.4
loaded via a namespace (and not attached):
[1] compiler_3.6.0 magrittr_1.5 tools_3.6.0 Rcpp_1.0.3 stringi_1.4.3 stringr_1.4.0
正如@akrun 所建议的,这是夏令时的问题。查看6
前后的日子:
format(testDate + days(c(5,7)), format = "%Y-%m-%d %H:%M:%S")
# [1] "2018-11-03 00:00:00" "2018-11-05 00:00:00"
您会注意到它假定 "midnight"(正如人们所期望的那样)。
2018年巴西圣保罗,4号(ref)半夜前滚:
When local standard time was about to reach Sunday, November 4, 2018, 12:00:00 midnight clocks were turned forward 1 hour to Sunday, November 4, 2018, 1:00:00 am local daylight time instead.
也就是说,11 月 3 日,23:59:59 存在,但它一向前一秒,就变成了 11 月 4 日,01:00:00。这表明(在数学上)11 月 3 日 00:00:00 从未存在过。
因此,R 通过返回 NA
.
但是,您可以很容易地处理 Date
个对象:
testDate + days(5:7)
# [1] "2018-11-03 -03" NA "2018-11-05 -02"
as.Date(testDate) + days(5:7)
# [1] "2018-11-03" "2018-11-04" "2018-11-05"