简体   繁体   English

用R和as.date格式化日期

[英]formatting dates with R and as.date

I'm trying to format dates with R. Can someone explain why R gives an error when these two strings are the exact same format? 我正在尝试使用R格式化日期。有人可以解释为什么当两个字符串完全相同时R会出错吗? I cannot figure out how to manage these date strings, and any help is greatly appreciated as I'm new working with R. 我无法弄清楚如何管理这些日期字符串,由于我是R的新人,因此非常感谢您的帮助。

> as.Date("09/18/2016 1:00 PM EDT")
Error in charToDate(x) : 
  character string is not in a standard unambiguous format
> as.Date("09/08/2016 8:30 PM EDT")
[1] "0009-08-20"

bad  <- as.Date("09/18/2016 1:00 PM EDT")
good <- as.Date("09/08/2016 8:30 PM EDT")

You missed the fact (and warning) that a non-standard format needs a format string for as.Date() . 您错过了一个事实(和警告),即非标准格式需要as.Date()的格式字符串。 And you also want strptime() if you want parse hours/minutes etc: 如果要解析小时/分钟等,也需要strptime()

R> strptime("09/18/2016 1:00 PM EDT", "%m/%d/%Y %I:%M %p")
[1] "2016-09-18 13:00:00 CDT"
R> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM