我有一个日期字符表示的向量,其中格式主要是dmY
(例如27-09-2013),dmy
(例如27-09-13),偶尔也有一些b
或B
几个月.因此,parse_date_time
在lubridate
"允许用户指定多个格式顺序以处理异构日期 - 时间字符表示"的包中对我来说可能是非常有用的功能.
但是,当它们与日期一起出现时,似乎parse_date_time
有解析dmy
日期的问题dmY
.dmy
单独解析,或dmy
与我相关的其他格式一起使用时,它可以正常工作.这种模式也在@ Peyton 在这里回答的评论中注明.建议快速修复,但我想问一下是否有可能处理它lubridate
.
在这里,我展示了一些示例,我尝试在dmy
格式上解析日期和其他一些格式,并相应地指定orders
.
library(lubridate)
# version: lubridate_1.3.0
# regarding how date format is specified in 'orders':
# examples in ?parse_date_time
# parse_date_time(x, "ymd")
# parse_date_time(x, "%y%m%d")
# parse_date_time(x, "%y %m %d")
# these order strings are equivalent and parses the same way
# "Formatting orders might include arbitrary separators. These are discarded"
# dmy date only
parse_date_time(x = "27-09-13", orders = "d m y")
# [1] "2013-09-27 UTC"
# OK
# dmy & dBY
parse_date_time(c("27-09-13", "27 September 2013"), orders = c("d m y", "d B Y"))
# [1] "2013-09-27 UTC" "2013-09-27 UTC"
# OK
# dmy & dbY
parse_date_time(c("27-09-13", "27 Sep 2013"), orders = c("d m y", "d b Y"))
# [1] "2013-09-27 UTC" "2013-09-27 UTC"
# OK
# dmy & dmY
parse_date_time(c("27-09-13", "27-09-2013"), orders = c("d m y", "d m Y"))
# [1] "0013-09-27 UTC" "2013-09-27 UTC"
# not OK
# does order of the date components matter?
parse_date_time(c("2013-09-27", "13-09-13"), orders = c("Y m d", "y m d"))
# [1] "2013-09-27 UTC" "0013-09-27 UTC"
# no
Run Code Online (Sandbox Code Playgroud)
这个select_formats
论点怎么样?很抱歉这样说,但我很难理解帮助文件的这一部分.并搜索select_formats
SO:0结果.尽管如此,这一部分似乎仍然相关:"默认情况下,选择格式最多的格式(%),%Y计为2.5个tockens(因此它可以优先于%y%m)." 所以我(拼命地)尝试了一些额外的dmy
日期:
parse_date_time(c("27-09-2013", rep("27-09-13", 10)), orders = c("d m y", "d m Y"))
# not OK. Tried also 100 dmy dates.
# does order in the vector matter?
parse_date_time(c(rep("27-09-13", 10), "27-09-2013"), orders = c("d m y", "d m Y"))
# no
Run Code Online (Sandbox Code Playgroud)
然后我检查如何guess_formats
功能(也lubridate
)处理dmy
一起dmY
:
guess_formats(c("27-09-13", "27-09-2013"), c("dmy", "dmY"), print_matches = TRUE)
# dmy dmY
# [1,] "27-09-13" "%d-%m-%y" ""
# [2,] "27-09-2013" "%d-%m-%Y" "%d-%m-%Y"
# OK
Run Code Online (Sandbox Code Playgroud)
来自?guess_formats
:y also matches Y
.来自?parse_date_time
:y* Year without century (00–99 or 0–99). Also matches year with century (Y format)
.所以我尝试过:
guess_formats(c("27-09-13", "27-09-2013"), c("dmy"), print_matches = TRUE)
# dmy
# [1,] "27-09-13" "%d-%m-%y"
# [2,] "27-09-2013" "%d-%m-%Y"
# OK
Run Code Online (Sandbox Code Playgroud)
因此,guess_format
似乎能够与之dmy
共处dmY
.但是我该如何判断parse_date_time
呢?提前感谢您的任何意见或帮助.
更新
我在lubridate
错误报告上发布了问题,得到了@vitoshka的快速回复:"这是一个错误".
它看起来像一个错误。我不确定所以你应该联系维护者。
构建包源并更改此内部函数中的一行(我替换which.max
为wich.min
):
.select_formats <- function(trained){
n_fmts <- nchar(gsub("[^%]", "", names(trained))) + grepl("%Y", names(trained))*1.5
names(trained[ which.min(n_fmts) ]) ## replace which.max by which.min
}
Run Code Online (Sandbox Code Playgroud)
似乎可以纠正这个问题。坦白说,我不知道为什么会这样,但我猜这是一种排名。
parse_date_time(c("27-09-13", "27-09-2013"), orders = c("d m y", "d m Y"))
[1] "2013-09-27 UTC" "2013-09-27 UTC"
parse_date_time(c("2013-09-27", "13-09-13"), orders = c("Y m d", "y m d"))
[1] "2013-09-27 UTC" "2013-09-13 UTC"
Run Code Online (Sandbox Code Playgroud)