First day of the month from a POSIXct date time using lubridate

nacnudus picture nacnudus · May 12, 2014 · Viewed 24.7k times · Source

Given a POSIXct date time, how do you extract the first day of the month for aggregation?

library(lubridate)

full.date <- ymd_hms("2013-01-01 00:00:21")

Answer

Frank picture Frank · Nov 12, 2015

lubridate has a function called floor_date which rounds date-times down. Calling it with unit = "month" does exactly what you want:

library(lubridate)
full.date <- ymd_hms("2013-01-01 00:00:21")
floor_date(full.date, "month")

[1] "2013-01-01 UTC"