本文介绍了R:当我尝试格式化日期字符串时,为什么strptime总是返回NA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的一些数据,从文件名AttReport_all中读取:Here's some of my data, read in from a file names AttReport_all:Registration.Date Join.Time Leave.Time1 Jul 05, 2011 09:30 PM EDT Jul 07, 2011 01:05 PM EDT Jul 07, 2011 01:53 PM EDT2 Jul 05, 2011 10:20 AM EDT Jul 07, 2011 01:04 PM EDT Jul 07, 2011 01:53 PM EDT3 Jul 04, 2011 02:41 PM EDT Jul 07, 2011 12:49 PM EDT Jul 07, 2011 01:53 PM EDT4 Jul 04, 2011 11:38 PM EDT Jul 07, 2011 12:49 PM EDT Jul 07, 2011 01:54 PM EDT5 Jul 05, 2011 11:41 AM EDT Jul 07, 2011 12:54 PM EDT Jul 07, 2011 01:54 PM EDT6 Jul 07, 2011 11:08 AM EDT Jul 07, 2011 01:16 PM EDT Jul 07, 2011 01:53 PM EDT如果我做 strptime(AttReport_all $ Registration.Date,%b%m,%Y%H:% M%p,tz =)我得到一个我期待日期的NAs数组。If I do strptime(AttReport_all$Registration.Date, "%b %m, %Y %H:%M %p", tz="") I get an array of NAs where I'm expecting dates. Sys.setlocale(LC_TIME,C)返回C typeof(AttReport_all $ Registration日期) re转为整数 is.factor(AttReport_all $ Registration.Date)返回TRUE。 我缺少什么?这里是版本输出,如果有帮助: platform i386- pc-mingw32 arch i386 os mingw32 system i386,mingw32 status major 2 minor 13.0 year 2011 month 04 day 13 svn rev 55427 语言R version.string R版本2.13.0(2011-04-13)Here's version output, if it helps:platform i386-pc-mingw32arch i386os mingw32system i386, mingw32statusmajor 2minor 13.0year 2011month 04day 13svn rev 55427language Rversion.string R version 2.13.0 (2011-04-13)推荐答案 strptime 在第一个参数上自动运行 as.character (所以这并不重要,这是一个因素) format = 中未指定的尾随字符将被忽略(因此EDT无关紧要)。strptime automatically runs as.character on the first argument (so it doesn't matter that it's a factor) and any trailing characters not specified in format= are ignored (so "EDT" doesn't matter).只有问题是打印机@Ben Bolker识别(%m 应该是%d )和 %H 应该是%I (?strptime 说你应该不使用%H 与%p )。The only issues are the typo @Ben Bolker identified (%m should be %d) and %H should be %I (?strptime says you should not use %H with %p).# %b and %m are both *month* formatsstrptime("Jul 05, 2011 09:30 PM EDT", "%b %m, %Y %H:%M %p", tz="")# [1] NA# change %m to %d and we no longer get NA, but the time is wrong (AM, not PM)strptime("Jul 05, 2011 09:30 PM EDT", "%b %d, %Y %H:%M %p", tz="")# [1] "2011-07-05 09:30:00"# use %I (not %H) with %pstrptime("Jul 05, 2011 09:30 PM EDT", "%b %d, %Y %I:%M %p", tz="")# [1] "2011-07-05 21:30:00" 这篇关于R:当我尝试格式化日期字符串时,为什么strptime总是返回NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 15:13