Nal-rA Nal-rA - 3 months ago 10
R Question

Lubridate: How to subtract the last observation of a month

I have a time series, and would like to get the information of the last observation of each month. This question is not about generating a new time series, but finding the last observation of each month among an existing time series. The last observation may not be the last day of a month. The following is just a small example,

date <- c(ymd(20010129, 20010228, 20010330, 20010429), ymd(20010501) + days(1:90))

# "2001-01-29" "2001-02-28" "2001-03-30" "2001-04-29" "2001-05-02" "2001-05-03" "2001-05-04" "2001-05-05"
# "2001-05-06" "2001-05-07" "2001-05-08" "2001-05-09" "2001-05-10" "2001-05-11" "2001-05-12" "2001-05-13"
# "2001-05-14" "2001-05-15" "2001-05-16" "2001-05-17" "2001-05-18" "2001-05-19" "2001-05-20" "2001-05-21"
# "2001-05-22" "2001-05-23" "2001-05-24" "2001-05-25" "2001-05-26" "2001-05-27" "2001-05-28" "2001-05-29"
# "2001-05-30" "2001-05-31" "2001-06-01" "2001-06-02" "2001-06-03" "2001-06-04" "2001-06-05" "2001-06-06"
# "2001-06-07" "2001-06-08" "2001-06-09" "2001-06-10" "2001-06-11" "2001-06-12" "2001-06-13" "2001-06-14"
# "2001-06-15" "2001-06-16" "2001-06-17" "2001-06-18" "2001-06-19" "2001-06-20" "2001-06-21" "2001-06-22"
# "2001-06-23" "2001-06-24" "2001-06-25" "2001-06-26" "2001-06-27" "2001-06-28" "2001-06-29" "2001-06-30"
# "2001-07-01" "2001-07-02" "2001-07-03" "2001-07-04" "2001-07-05" "2001-07-06" "2001-07-07" "2001-07-08"
# "2001-07-09" "2001-07-10" "2001-07-11" "2001-07-12" "2001-07-13" "2001-07-14" "2001-07-15" "2001-07-16"
# "2001-07-17" "2001-07-18" "2001-07-19" "2001-07-20" "2001-07-21" "2001-07-22" "2001-07-23" "2001-07-24"
# "2001-07-25" "2001-07-26" "2001-07-27" "2001-07-28" "2001-07-29" "2001-07-30"


I want to keep the observation of
"2001-01-29"
,
"2001-02-28"
,
"2001-03-30"
,
"2001-04-29"
,
"2001-05-31"
,
"2001-06-30"
, and
"2001-07-30"
. Is there a way to achieve it?

Answer

You can group the date by month and calculate the maximum value:

library(lubridate)
unique(ave(date, month(date), FUN = max))

# [1] "2001-01-29" "2001-02-28" "2001-03-30" "2001-04-29"
# [5] "2001-05-31" "2001-06-30" "2001-07-30"
Comments