在数据框中,有一列以日期信息为字符
str(mv$datum_mw)
chr [1:6] "Nov 15, 2012" "Jan 28, 2013" "Aug 12, 2014" "Feb 12, 2015" "Jul 1, 2015" "Feb 9, 2016"
将此字符转换为常规日期格式的所有尝试都会产生“NA”
mv$new_date_column <- as.Date(mv$datum_mw, format = "%b ,%d %Y")
mv$new_date_column <- format(mv$new_date_column, "%b-%y %Y")
或
dates <- as.Date(mv$datum_mw, "%m/%d/%Y")
使用
mdy
中的lubridate
,只要保持月、日、年顺序即可。
library(lubridate)
mdy(c("Nov 15, 2012", "Jan 28, 2013", "Aug 12, 2014", "Feb 12, 2015",
"Jul 1, 2015", "Feb 9, 2016", "031124", "01-12-89", "4/2/2011"))
[1] "2012-11-15" "2013-01-28" "2014-08-12" "2015-02-12" "2015-07-01"
[6] "2016-02-09" "2024-03-11" "1989-01-12" "2011-04-02"