分析日期时间变量的日间时间

Analyse day-time of a date-time variable

我是这样解决问题的:

datestimes <- c("2014-01-01 23:03:00", "2014-01-02 00:35:00", "2014-01-02 00:51:00") # There is a change in date. 
# Is there any lubridate command for the following step?    
time <- as.POSIXct(strftime(datetimes, format = "%H:%M:%S", tz = "UTC"), format = "%H:%M:%S") 
time  
[1] "2016-05-13 23:03:00 CEST" "2016-05-13 00:35:00 CEST" "2016-05-13 00:51:00 CEST"

lubridate 中没有这样的功能是有原因的吗? 正如我所说 - 我只对时间部分感兴趣 - 而不是日期。

lubridate::hms 给出句点:

ltime <- lubridate::hms(strftime(datetimes, format = "%H:%M:%S", tz = "UTC"))  
ltime  
[1] "23H 3M 0S" "35M 0S"    "51M 0S"  
class(ltime)  
[1] "Period"  
attr(,"package")  
[1] "lubridate"

这个呢?

library(lubridate)
datetimes <- c("2014-01-01 23:03:00", "2014-01-02 00:35:00", "2014-01-02 00:51:00") 
dataset <- data.frame(
  time = as.POSIXct(strftime(datetimes, format = "%H:%M:%S", tz = "UTC"), format = "%H:%M:%S") 
)
dataset$delta <- dataset$time - floor_date(dataset$time, unit = "day")
dataset$relative <- as.POSIXct("2001-01-01 0:0:0") + minutes(dataset$delta)

library(ggplot2)
ggplot(dataset, aes(x = relative)) + 
  geom_histogram(binwidth = 3600) + 
  scale_x_datetime(date_breaks = "3 hour", date_labels = "%H:%M")

感谢 alistair:ymd_hms(paste(today(), format(ymd_hms(datetimes), '%T'))) 将是我的选择。这可以与 histggplot2 很好地结合(见上文)。