0

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.stack.imgur.com/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!!!!!

  • The best answer for your case probably depends on what format your data are in. There is the `apply.monthly` function for xts objects, for example. If your data are in a time series format, you could then take the last value by month in combination with the `tail` function. – BenBarnes Jan 16 '19 at 15:25
  • So please post a data example, preferably the output of `dput(fundbv)`or a subset of your data. – BenBarnes Jan 16 '19 at 15:27
  • Thanks for your helpful,now I fixed one problem,but comes a bigger problem...... – Chon Kit Hui Jan 16 '19 at 20:04
  • that's why it's infinitely more helpful if you post the data you're working with or a representative part of it. Please see [this SO question and answers](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for guidance – BenBarnes Jan 17 '19 at 05:49

1 Answers1

1

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))
G. Belton
  • 133
  • 8
  • Thanks for your help!However,I found another bigger problem......In my panel,some funds have a different last day of month,I used the code ignore many last days and their data.....oh my godQ_Q – Chon Kit Hui Jan 16 '19 at 20:02