在一系列日期中查找月份的最后一天 [英] find last day of month in a sequence of dates

查看:65
本文介绍了在一系列日期中查找月份的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列这样的日期

ds <- seq(as.Date("2011-02-01"), length=100, by="day")

我想找到每月最后几天的索引

I want to find the indices of the last days of each month

我可以这样做

last_day <- seq(as.Date("2011-02-01"), length=10, by="1 month") - 1

which(ds %in% last_day)

我的问题是我的日期顺序不完整,缺少某些日期,有时可能是最后一天.

my problem is that my sequence of dates is not complete, some dates are missing and sometimes this can be the last day.

例如,我删除了2月的最后一天

For example, I removed the last day of February

ds[ds == as.Date('2011-02-28')] <- NA

新的最后一天现在应该是"2011-02-27".

The new last day should now be '2011-02-27'.

如何根据向量中的日期找到每个月的最后一个? 日期跨越数年.

How can I find the last of for each month based on the dates in my vector? The dates span over several years.

推荐答案

尝试:

which(ave(as.numeric(ds),format(ds,"%Y%m"),FUN=function(x) x==max(x))==1)

这篇关于在一系列日期中查找月份的最后一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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