使用不同类型的时间格式将字符串转换为日期
Convert string to date with different types of time format
我有如下数据框
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.
如何解决?
试试这个:
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。
我有如下数据框
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.
如何解决?
试试这个:
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。