问题
I have a sequence of dates like this
ds <- seq(as.Date("2011-02-01"), length=100, by="day")
I want to find the indices of the last days of each month
I can do it like this
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.
For example, I removed the last day of February
ds[ds == as.Date('2011-02-28')] <- NA
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.
回答1:
Try:
which(ave(as.numeric(ds),format(ds,"%Y%m"),FUN=function(x) x==max(x))==1)
回答2:
We can group_by
month and select the max
date from each month
library(zoo)
library(dplyr)
data.frame(ds) %>%
group_by(month = as.yearmon(ds)) %>%
slice(which.max(ds))
# ds month
# <date> <S3: yearmon>
#1 2011-02-27 Feb 2011
#2 2011-03-31 Mar 2011
#3 2011-04-30 Apr 2011
#4 2011-05-11 May 2011
If we want the indices, we can do
library(zoo)
which(ds %in% unique(ave(ds, as.yearmon(ds), FUN = max)))
#[1] 27 58 88 99
回答3:
Function nth_day
in package datetimeutils (which I maintain)
allows you to get the last day of a month. It won't handle NA
values, though.
library("datetimeutils")
ds <- seq(as.Date("2011-02-01"), length = 100, by = "day")
nth_day(ds, n = "last")
## [1] "2011-02-28" "2011-03-31" "2011-04-30" "2011-05-11"
nth_day(ds, n = "last", index = TRUE)
## [1] 28 59 89 100
回答4:
Using endpoints
from xts package:
ds <- seq(as.Date("2011-02-01"), length=100, by="day")
ds[ds == as.Date('2011-02-28')] <- NA
library(xts)
#need to remove NA's. xts can handle dates that are not there, but doesn't like NA's
ep <- endpoints(xts(ds[!is.na(ds)], order.by = ds[!is.na(ds)]), on = "months")
ds[ep]
[1] "2011-02-27" "2011-03-30" "2011-04-29" "2011-05-10"
来源:https://stackoverflow.com/questions/52110221/find-last-day-of-month-in-a-sequence-of-dates