Date
01/01/2013 #mm/dd/yyyy
12122015 #mmddyyyy
2014-03-21 #yyyy-mm-dd
95.10.12 #yy.mm.dd
我有一个不同格式的“日期”列。如何清理并将其转换为单一日期格式? 附加信息:class(Date)是factor。
答案 0 :(得分:2)
最简单的方法是使用lubridate
包。
Date=c(
"01/01/2013" #mm/dd/yyyy
,"12122015" #mmddyyyy
,"2014-03-21" #yyyy-mm-dd
,"95.10.12" #yy.mm.dd
)
library(lubridate)
# list of functions in lubridate that
# translate text to POSIXct: you only need to know the
# order of the data (e.g. mdy = month-day-year).
funs <- c("mdy","dym","ymd","dmy","myd","ydm")
# vector to store results
dates <- as.POSIXct(rep(NA,length(Date)))
# we try everything lubridate has. There will be some warnings
# e.g. because mdy cannot translate everything. You can ignore this.
for ( f in funs ){
dates[is.na(dates)] <- do.call(f,list(Date[is.na(dates)]))
}
dates
> dates
[1] "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET" "2013-01-01 01:00:00 CET" "2015-12-12 01:00:00 CET"