我想知道是否有人知道一个包或函数可以检测 R 中日期列格式的任何中断。即检测日期向量格式更改的位置,例如:

11/2/90
12/2/90
.
.
.
15/Feb/1990
16/Feb/1990
.
.
.
20/February/90
21/February/90
.
.
.
25/2/1990
26/2/1990

最佳答案

您是否只需要检测中断点,还是最终也想转换这些中断点?
guess_formats 包中的 lubridate 函数在这两种情况下都很有用。从您的数据中查看此示例:

dates = c("11/2/90",
          "12/2/90",
          "15/Feb/1990",
          "16/Feb/1990",
          "20/February/90",
          "21/February/90",
          "25/2/1990",
          "26/2/1990")

guess_formats(dates, order="dmy")
       dmy        dmy        dmy        dmy        dmy        dmy        dmy        dmy
"%d/%m/%y" "%d/%m/%y" "%d/%b/%Y" "%d/%b/%Y" "%d/%B/%y" "%d/%B/%y" "%d/%m/%Y" "%d/%m/%Y"

dates2 = as.Date(dates, format=guess_formats(dates, order="dmy")
dates2
[1] "1990-02-11" "1990-02-12" "1990-02-15" "1990-02-16" "1990-02-20" "1990-02-21" "1990-02-25" "1990-02-26"

关于r - 检测日期列格式的中断/更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24158049/

10-11 07:54