生成两年内每月最后一天的序列 [英] Generate a sequence of the last day of the month over two years

查看:25
本文介绍了生成两年内每月最后一天的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 lubridate 并认为这很容易

ymd("2010-01-31")+months(0:23)

但是看看得到了什么.全搞砸了!

 [1] "2010-01-31 UTC" "2010-03-03 UTC" "2010-03-31 UTC" "2010-05-01 UTC" "2010-05-31 UTC" "2010-07-01 UTC" "2010-07-31 UTC" "2010-08-31 UTC" "2010-10-01 UTC"[10] "2010-10-31 UTC" "2010-12-01 UTC" "2010-12-31 UTC" "2011-01-31 UTC" "2011-03-03 UTC" "2011-03-31 UTC" "2011-05-01 UTC" "2011-05-31 UTC" "2011-07-01 UTC"[19] "2011-07-31 UTC" "2011-08-31 UTC" "2011-10-01 UTC" "2011-10-31 UTC" "2011-12-01 UTC" "2011-12-31 UTC"

然后我读了 lubridate 如何迎合诸如间隔、持续时间和周期等现象.所以,好吧,我意识到一个月实际上是由 (365*4+1)/48 = 30.438 天定义的天数.所以我试着变得聪明并将其重写为

ymd("2010-01-31")+ as.period(months(0:23))

但这只是给了一个错误.

<块引用>

 as.period.default(months(0:23)) 中的错误:(list) 对象不能被强制输入double"

解决方案

是的,您找到了正确的技巧:从 下个月的第一天返回一天.

这是在基础 R 中的单行:

R>seq(as.Date("2010-02-01"), length=24, by="1 个月") - 1[1] "2010-01-31" "2010-02-28" "2010-03-31" "2010-04-30" "2010-05-31"[6] "2010-06-30" "2010-07-31" "2010-08-31" "2010-09-30" "2010-10-31"[11] "2010-11-30" "2010-12-31" "2011-01-31" "2011-02-28" "2011-03-31"[16] "2011-04-30" "2011-05-31" "2011-06-30" "2011-07-31" "2011-08-31"[21] 《2011-09-30》 《2011-10-31》 《2011-11-30》 《2011-12-31》R>

所以不需要 lubridate 这(虽然是一个很好的包)不是像这样的简单任务需要.另外,它对现有基本函数的重载仍然让我觉得有些危险......

I use lubridate and figured that this would be so easy

ymd("2010-01-31")+months(0:23)

But look what one gets. It is all messed up!

 [1] "2010-01-31 UTC" "2010-03-03 UTC" "2010-03-31 UTC" "2010-05-01 UTC" "2010-05-31 UTC" "2010-07-01 UTC" "2010-07-31 UTC" "2010-08-31 UTC" "2010-10-01 UTC"
[10] "2010-10-31 UTC" "2010-12-01 UTC" "2010-12-31 UTC" "2011-01-31 UTC" "2011-03-03 UTC" "2011-03-31 UTC" "2011-05-01 UTC" "2011-05-31 UTC" "2011-07-01 UTC"
[19] "2011-07-31 UTC" "2011-08-31 UTC" "2011-10-01 UTC" "2011-10-31 UTC" "2011-12-01 UTC" "2011-12-31 UTC"

Then I read how lubridate caters to phenomenon such as interval, duration and period. So, OK I realize that a month is actually the number of days defined by (365*4+1)/48 = 30.438 days. So I tried to get smart and rewrite it as

ymd("2010-01-31")+ as.period(months(0:23))

But that just gave an error.

Error in as.period.default(months(0:23)) : 
  (list) object cannot be coerced to type 'double'

解决方案

Yes, you found the correct trick: going back a day from the first of the next month.

Here is as a one-liner in base R:

R> seq(as.Date("2010-02-01"), length=24, by="1 month") - 1
 [1] "2010-01-31" "2010-02-28" "2010-03-31" "2010-04-30" "2010-05-31"
 [6] "2010-06-30" "2010-07-31" "2010-08-31" "2010-09-30" "2010-10-31"
[11] "2010-11-30" "2010-12-31" "2011-01-31" "2011-02-28" "2011-03-31"
[16] "2011-04-30" "2011-05-31" "2011-06-30" "2011-07-31" "2011-08-31"
[21] "2011-09-30" "2011-10-31" "2011-11-30" "2011-12-31"
R> 

So no need for lubridate which (while being a fine package) isn't needed for simple task like this. Plus, its overloading of existing base functions still strikes me as somewhat dangerous...

这篇关于生成两年内每月最后一天的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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