user3655531 user3655531 - 1 day ago 4
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
2013-12-24 01:00:00


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

Answer

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

Comments