在一系列日期中查找月份的最后一天

编程入门 行业动态 更新时间:2024-10-27 10:25:15
本文介绍了在一系列日期中查找月份的最后一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一系列这样的日期

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)

更多推荐

在一系列日期中查找月份的最后一天

本文发布于:2023-10-24 09:21:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在一   日期   系列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!