我有一个数据帧,它的日期列是一个char类。我试过解析为。日期,但NAs的数量令人担忧。日期格式为:"2003-10-19"one_answers"October 05, 2018">
date <- c("October 05, 2018", "2003-10-19")
as.Date(date)
这是我所尝试的,但我的大多数结果都返回了NAs
这是一个选项:
date <- c("October 05, 2018", "2003-10-19", "10/9/95", "6 Oct.2010")
lubridate::parse_date_time(date, orders = c("mdy", "ymd", "dmy"))
#> [1] "2018-10-05 UTC" "2003-10-19 UTC" "1995-10-09 UTC" "2010-10-06 UTC"
as.Date
有一个称为tryFormats
的特性,它不是矢量化的,但可以与例如lapply
一起使用。
date <- c("October 05, 2018", "2003-10-19", "02/04/20", "11/09/2002",
"14.05.2021", "Nov 1, 2022", "March 1, 2004")
lapply(date, as.Date, tryFormats=c("%Y-%m-%d", "%B %d, %Y", "%d/%m/%y",
"%m/%d/%Y", "%d.%m.%Y", "%b %d, %Y"))
[[1]]
[1] "2018-10-05"
[[2]]
[1] "2003-10-19"
[[3]]
[1] "2020-04-02"
[[4]]
[1] "2020-09-11"
[[5]]
[1] "2021-05-14"
[[6]]
[1] "2022-11-01"
[[7]]
[1] "2004-03-01"