Lubridate not converting datetime to POSIXct correctly in R (dd/mm/yy hh:mm:ss) [closed]

故事扮演 提交于 2021-02-20 05:22:07

问题


I'm trying to convert my datetime from a csv to POSIXct for data analysis. I've tried multiple codes but either get NA or a wrong format.

The code I'm currently using is

GRS$datetimelocal<- 
 GRS$`datetime` %>%
 ymd_hms(tz="UTC") %>% # first convert the `Date and Time (UTC)` 
 column into a 'POSIX' format 
 with_tz(tzone="Australia/Brisbane") # convert to local 
"Australia/Brisbane" date time (UTC + 10hrs)

My datetime column is in the format dd/mm/yy hh:mm:ss.

datetime
26/03/2013 21:50
26/03/2013 21:56
26/03/2013 21:58
28/03/2013 07:42

However the new column spits out as

datetimelocal 
2026-03-20 13:21:50
2026-03-20 13:21:56
2026-03-20 13:21:58
2028-03-20 13:07:42

Any help would be much appreciated


回答1:


Why not use as.POSIXct directly?

as.POSIXct("26/03/2013 21:50", 
           format = "%d/%m/%Y %H:%M", 
           tz = "Australia/Brisbane")
## [1] "2013-03-26 21:50:00 AEST"


来源:https://stackoverflow.com/questions/49919618/lubridate-not-converting-datetime-to-posixct-correctly-in-r-dd-mm-yy-hhmmss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!