我对日期结束有疑问,我将在下面解释.
这是我的示例数据:
DATE
2015-01-01
2015-02-05
2015-09-29
2016-02-07
2016-07-24
2016-12-16
Run Code Online (Sandbox Code Playgroud)
我知道如果我想要那个月的总天数,代码是:
days_in_month(DATE)
Run Code Online (Sandbox Code Playgroud)
但是,我想要的是如下:
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
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
Dav*_*son 12
你可以这样做:
library(lubridate)
DATE$DATE_Month_End <- DATE$DATE
day(DATE$DATE_Month_End) <- days_in_month(DATE$DATE)
Run Code Online (Sandbox Code Playgroud)
因为day() <-让你在保持年月的同时改变一天.