【发布时间】:2016-05-13 06:54:41
【问题描述】:
这就是我解决问题的方法:
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"
【问题讨论】:
-
您能否更清楚地描述您的问题?没有日期的时间并没有很好地定义,因为会有 DST 甚至偶尔的闰秒等不便之处。
-
lubridate没有没有日期的时间类(除了句点,虽然你可以工作,但有点不同)。您可以查看chron。 -
Well lubridate 可以提取小时、分钟、秒。您也可以使用正则表达式来获取时间组件。
-
我猜你可以用
ymd_hms(paste(today(), format(ymd_hms(datetimes), '%T')))做同样的事情......但我不确定这是一个很好的方法。 -
@Roland:假设
datetimes是瞬间,电影院大楼里的人都算在内。现在你要分析,人们什么时候去电影院,取决于白天的时间。你有比 alistaire 更好的主意吗? @alistaire:你的方法似乎完全符合我的需要——而且它告诉你用today覆盖日期是准确的。