Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R calculate month end [duplicate]

Tags:

date

r

I have a question about date end, I explain in the following.
Here is my example data:

DATE
2015-01-01
2015-02-05
2015-09-29
2016-02-07
2016-07-24
2016-12-16

I know if I want the total days in that month, the code is:

days_in_month(DATE)

However, what I want is as following:

DATE            DATE_Month_End
2015-01-01      2015-01-31
2015-02-05      2015-02-28
2015-09-29      2015-09-30
2016-02-07      2016-02-29
2016-07-24      2016-07-31
2016-12-16      2016-12-31

any suggestion?

like image 577
Peter Chen Avatar asked Nov 30 '22 09:11

Peter Chen


1 Answers

You can do this with:

library(lubridate)

DATE$DATE_Month_End <- DATE$DATE
day(DATE$DATE_Month_End) <- days_in_month(DATE$DATE)

since day() <- lets you change the day while keeping the year and month.

like image 173
David Robinson Avatar answered Dec 15 '22 12:12

David Robinson