R帮助转换日期时间

时间:2019-06-17 03:10:39

标签: r datetime lubridate

嗨,我在df中有以下几列,希望将它们都转换为相同类型的日期格式。这样一来,我就可以创建一个新的col持续时间,其中需要使用“ End.Date.Time”-“ Start.Date.Time”:

End.Date.Time    Start.Date.Time
1/5/2018 12:14   20171125 
1/5/2018 12:14   20171125 
1/5/2018 12:14   20171125 
30/5/2017 22:47  20170502
30/5/2017 22:47  20170502
30/5/2017 22:47  20170502

我尝试了

library(lubridate)
df %>% mutate(End.Date.Time = ymd_hms(End.Date.Time), Start.Date.Time = ymd_hms(Start.Date.Time))

out: All formats failed to parse. No formats found.

“开始日期时间”值全部变为NA。 我该怎么解决?

3 个答案:

答案 0 :(得分:2)

Base R方式将是

transform(df, 
   End.Date.Time  = as.POSIXct(End.Date.Time, format = "%d/%m/%Y %H:%M"), 
   Start.Date.Time = as.POSIXct(as.character(Start.Date.Time), format = "%Y%m%d"))

#        End.Date.Time Start.Date.Time
#1 2018-05-01 12:14:00      2017-11-25
#2 2018-05-01 12:14:00      2017-11-25
#3 2018-05-01 12:14:00      2017-11-25
#4 2017-05-30 22:47:00      2017-05-02
#5 2017-05-30 22:47:00      2017-05-02
#6 2017-05-30 22:47:00      2017-05-02

或者如果您希望将这两列都作为“日期”,则可以这样做

transform(df, 
  End.Date.Time  = as.Date(as.POSIXct(End.Date.Time, format = "%d/%m/%Y %H:%M")), 
  Start.Date.Time = as.Date(as.character(Start.Date.Time), format = "%Y%m%d"))

借助lubridate,我们可以使用函数dmy_hmymd进行转换

library(dplyr)
library(lubridate)

df %>% 
   mutate(End.Date.Time = dmy_hm(End.Date.Time), 
          Start.Date.Time = as.POSIXct(ymd(Start.Date.Time)))

数据

df <- structure(list(End.Date.Time = structure(c(1L, 1L, 1L, 2L, 2L, 
2L), .Label = c("1/5/2018 12:14", "30/5/2017 22:47"), class = "factor"), 
Start.Date.Time = c(20171125L, 20171125L, 20171125L, 20170502L, 
20170502L, 20170502L)), class = "data.frame", row.names = c(NA, -6L))

答案 1 :(得分:1)

如果您想坚持使用lubridate,下面的代码应该会有所帮助。

df = structure(list(End.Date.Time = c("1/5/2018 12:14", "1/5/2018 12:14", 
                                  "1/5/2018 12:14", "30/5/2017 22:47", 
                                  "30/5/2017 22:47", "30/5/2017 22:47"), 
                Start.Date.Time = c(20171125L, 20171125L, 20171125L, 20170502L, 
                                    20170502L, 20170502L)), 
                .Names = c("End.Date.Time", "Start.Date.Time"), 
                row.names = c(NA, -6L), class = "data.frame")


df %>% 
  mutate(End.Date.Time = dmy_hm(End.Date.Time) %>% date, #dmy_hm outputs datetime. 
         Start.Date.Time = ymd(Start.Date.Time))

和as.Date与base也应该起作用。

df %>% 
  mutate(End.Date.Time = dmy_hm(End.Date.Time) %>% as.Date, 
         Start.Date.Time = ymd(Start.Date.Time))

答案 2 :(得分:0)

大家好,非常感谢您的解决方案。我做了以下并得到了我需要的东西:

df$Duration <- as.Date((ymd_hms(df$End.Date.Time))) - (ymd(df$Start.Date.Time))