我想转换数据框
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
答案 0 :(得分:3)
一种选择是在"POSIXt"
课程中获取您的日期,然后转换为"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"