我想转换数据框
12/31/2012 23:49:18
12/30/2012 20:27:17
12/29/2012 20:21:24
至
2012-12-31
2013-12-30
2013-12-29
一种选择是将您的日期放在一个"POSIXt"
类中,然后转换为 a "Date"
,强制时区避免在转换过程中出现问题:
dates <- as.POSIXct(c("12/31/2012 23:49:18", "12/30/2012 20:27:17",
"12/29/2012 20:21:24"),
format = "%m/%d/%Y %H:%M:%S", tz = "UTC")
as.Date(dates)
R> as.Date(dates)
[1] "2012-12-31" "2012-12-30" "2012-12-29"
另一种方法是将dates
(如上所述)格式化为您需要的正确格式:
R> format(dates, format = "%Y-%m-%d")
[1] "2012-12-31" "2012-12-30" "2012-12-29"