解析压缩日期时间,其中年和小时之间没有分隔符

parsing compacted date times where no separating character between year and hour

我尝试了几种不同的方法在 R 中为此日期时间解析日期时间,lubridate::mdy_hm 似乎有效,但也有一个奇怪的行为,即不处理数组的单个元素?

datetimes <- c("10/6/20176:00 PM EDT", "10/16/20171:00 PM EDT", "10/6/201711:00 PM EDT", "10/16/201711:00 PM EDT")

substrRight <- function(x, n){
  substr(x, nchar(x)-n+1, nchar(x))
}

(time_with_bad_nas <- substrRight(datetimes, 11)) # two should be 11:00 PM ET
(date_with_bad_nas <- substr(datetimes, 0, 10)) # two are capturing the hour in the year 

lubridate::mdy_hm(datetimes[1], tz = "America/New_York")
lubridate::mdy_hm(datetimes, tz = "America/New_York")

datetimes[1] == "10/6/20176:00 PM ET"

可以试试:

as.POSIXct(datetimes, format = "%m/%d/%Y%I:%M %p", tz = "America/New_York")

输出:

 "2017-10-06 18:00:00 EDT" "2017-10-16 13:00:00 EDT" "2017-10-06 23:00:00 EDT" "2017-10-16 23:00:00 EDT"