view raw
user3655531 user3655531 - 5 months ago 36
R Question

Add hour if missing in timestamp using lubridate

I have a list of timestamps in the factor format the I want to convert using lurbridate.

However some of the timestamps lack time 00:00:00:

2013-12-24 23:00:00
2013-12-24 01:00:00

How do I expand the
df$timestamp <- ymd_hms(df$Timestamp_factor)
to insert
if time i missing?


You can use the truncated term for lubridate to get those missing parameters. In this case, you are missing three parameters hour, minute and second

ymd_hms(c("2013-12-24 23:00:00", "2013-12-24", "2013-12-24 01:00:00"), truncated = 3)

That, however, will always return 00:00:00 as the time