使用不同类型的时间格式将字符串转换为日期

时间:2016-11-21 18:43:36

标签: r lubridate

我有一个数据框如下:

library(lubridate) 
Date <- c("18.11.2016 21:03:41", "19.11.2016", "20.11.2016","21.11.2016")
df = data.frame(Date) 
df

我得到

df$Date
[1] "2016-11-18" "2016-11-19" "2016-11-20" "2016-11-21"

&安培;尝试将它转换为像这样的日期

df$Date = dmy(df$Date)

我得到了

Warning message:
 1 failed to parse. 

如何解决?

1 个答案:

答案 0 :(得分:0)

试试这个:

s <- c("2004-03-21 12:45:33.123456",    # ISO
       "2004/03/21 12:45:33.123456",    # variant
       "20040321",                      # just dates work fine as well
       "Mar/21/2004",                   # US format, also support month abbreviation or full
       "rapunzel")                      # will produce a NA

p <- toPOSIXct(s)

options("digits.secs"=6)                # make sure we see microseconds in output
print(format(p, tz="UTC")

更多地了解here