Chon Kit Hui
Chon Kit Hui

Reputation: 65

Filte the month end date of data

I'm trying to filte some daily panel ,but I just want to use the month end data,first I must know their month end date.

data example: https://i.sstatic.net/Qzhlr.jpg

I tried to use this code to get the month end date.However,some dates are missed (Some month end days are 24/25/26,I missed many data) My problems is,how can I get the data month end date and not ignore any earlier last day(like3/23,6/25,etc.)

library(anytime)
x=anydate(as.vector(fundbv$date))
y=unique(as.Date(format(mydates+28,"%Y-%m-01"))-1)
finaldays=x[x %in% unique(as.Date(format(x+28,"%Y-%m-01"))-1)]
finaldays=unique(finaldays)

Thanks and appreciate!!!!!

Upvotes: 0

Views: 64

Answers (1)

G. Belton
G. Belton

Reputation: 133

Here's how to do it with dplyr and lubridate:

library(dplyr)
library(lubridate)

# generate a data frame with dates to play with
(df <- data_frame(
  date=seq(as.Date("2017-01-01"), as.Date("2018-12-31"), by=6),
  amount=rgamma(length(date), shape=2, scale=20)))

df %>%
  group_by(month=floor_date(date, "month")) %>%
  summarize(date = max(date))

Upvotes: 1

Related Questions