Formatting Date Strings in R -
i have 2 columns of differently formatted date strings need make same format,
the first in form:
vt_dev_date = "6/20/2016 7:45"
the second in form
vt_other = "2016-06-14 20:21:29.0"
if them both in same form down minute great. have tried
strptime(vt_dev_date,format = "%y-%m-%d %h:%m") strptime(vt_other,"%y-%m-%d %h:%m")
and second one, works , "2016-06-14 20:21:00 edt"
but first string, seems because month , hour not padded zeros, none of formating tricks work, becuase if try
test_string <- "06/20/2016 07:45" strptime(test_string,format = "%m/%d/%y %h:%m") [1] "2016-06-20 07:45:00 edt"
it works, dont think going through every row in column , padding each date great option. appreciated.
thanks,
josh
how using lubridate , follows :
library(lubridate) x <- c("6/20/2016 7:45","2016-06-14 20:21:29.0") > x [1] "6/20/2016 7:45" "2016-06-14 20:21:29.0" > parse_date_time(x, orders = c("mdy hm", "ymd hms")) [1] "2016-06-20 07:45:00 utc" "2016-06-14 20:21:29 utc" >
Comments
Post a Comment