我在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"
答案 0 :(得分:3)
可以尝试:
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"