R计算月末 [英] R calculate month end

查看:90
本文介绍了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)

不过,我想要的是:

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

有什么建议吗?

推荐答案

您可以使用以下方法:

library(lubridate)

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

因为 day()<-允许您更改日期,同时保留年份和月份。

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

这篇关于R计算月末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆