我发现日期不一致的向量,主要包括以下三种格式: “%d /%m /%y”,“%m /%d /%y”和“%d /%m /%Y”
我试图实现这一点:
df <- as.data.frame(c("30/12/00","7/31/09","17/09/2008"),col.names = "original_date")
guess_date <- function(x){
require(lubridate)
guess <- guess_formats(x, c("mdy","dmy"))
date <- as.Date(x, guess)[1]
return(date)
}
df$date <- lapply(df$original_date, guess_date)
答案 0 :(得分:1)
我们可以用parse_date_time
library(lubridate)
parse_date_time(df$original_date,
guess_formats(as.character(df$original_date), c("mdy", "dmy", "dmY")))
#[1] "2000-12-30 UTC" "2009-07-31 UTC" "2008-09-17 UTC"