-1

我想转换数据框

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

4

1 回答 1

4

一种选择是将您的日期放在一个"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"
于 2013-10-28T21:50:51.783 回答