2017-09-26 6 views
0

J'ai essayé quelques méthodes différentes à l'analyse datetime dans R pour ce datetime, lubridate :: mdy_hm semble fonctionner mais a également un comportement étrange de ne pas gérer un seul élément d'un tableau?l'analyse de la date compacte fois où aucun caractère séparant entre l'année et l'heure

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

'(\ d +) \/(\ d +) \/(\ d {4}) (\ d +) : (\ d +) \ s + ([ap] m) \ s + ([az] +) 'comme vu [ici] (https://regex101.com/r/2WWKvv/3) obtient chaque partie. Si vous avez besoin de formats 2 ans, utilisez ceci: '(\ d +) \/(\ d +) \/(\ d {4} | \ d {2}) (\ d +): (\ d +) \ s + ([ap] m) \ s + ([az] +) ': https://regex101.com/r/2WWKvv/2 – ctwheels

Répondre

3

On pourrait essayer:

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

sortie:

"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"