0

How can I transform a date from the beggning of the period, to the final of the period in R language? Like follow:

Switch 2000-01-01 to 2000-12-01

Example:

df$date

date
2009-01-01
2010-01-01
2011-01-01
2012-01-01
.
.
.

Desired output:

df$date

date
2009-12-01
2010-12-01
2011-12-01
2012-12-01
.
.
.

  • 2
    Hi Jecksom and welcome to SO. What's exactly do you mean by "period?" – rdelrossi Feb 17 '22 at 00:25
  • 1
    @Jecksom Gomes I think you are looking for ceiling_date(), which takes a date-time object and rounds it up to the nearest boundary of the specified time unit. See package lubridate. https://lubridate.tidyverse.org/reference/round_date.html – Mel G Feb 17 '22 at 00:32
  • `as.Date('2000-01-01') + lubridate::years(1) - lubridate::days(1)` – Onyambu Feb 17 '22 at 01:12
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It's hard to guess exactly what you want from a single data point. – MrFlick Feb 17 '22 at 02:06
  • I managed to solve my question with: `df$date <- as.Date(df$date) + lubridate::years(1) - lubridate::days(31)`, thank you @Onyambu!! thank you all!! – Jecksom Gomes Feb 17 '22 at 11:58

0 Answers0