【问题标题】:Lubridate not converting datetime to POSIXct correctly in R (dd/mm/yy hh:mm:ss) [closed]Lubridate 没有在 R (dd/mm/yy hh:mm:ss) 中正确地将日期时间转换为 POSIXct [关闭]
【发布时间】:2018-04-19 11:09:36
【问题描述】:

我正在尝试将我的日期时间从 csv 转换为 POSIXct 以进行数据分析。我尝试了多个代码,但要么得到 NA 要么格式错误。

我目前使用的代码是

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)

我的日期时间列的格式为 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

然而新的列却是

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

任何帮助将不胜感激

【问题讨论】:

  • 而不是ymd_hms 使用dmy_hm
  • 啊,非常感谢。我以为我之前已经尝试过,但我可以添加 dmy_hms 代替。非常感谢

标签: r datetime lubridate


【解决方案1】:

为什么不直接使用as.POSIXct

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

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多