0

I have a sequence of dates and each date has a value

dat = data.frame(date = seq(as.Date("2015/1/1"), as.Date("2016/8/1"), "days"), value =seq(1, length(seq(as.Date("2015/1/1"), as.Date("2016/8/1"), "days")),1 ))

I'd like to subset the dataframe and return the last day in the quarter with a value

So for that dates above the results should be

2015-03-31    90
2015-06-30   181
2015-09-30   273
2015-12-31   365
2016-03-31   456
2016-06-30   547
2016-08-01   579
user3022875
  • 8,598
  • 26
  • 103
  • 167
  • What is your reference date? `2015-01-01`? Have a look at [that](http://rpackages.ianhowson.com/cran/lubridate/man/quarter.html) – Christoph Sep 06 '16 at 19:00
  • https://www.google.com/?client=ubuntu#channel=fs&q=site%3Astackoverflow.com+[r]+last+day+of+quarter – Rich Scriven Sep 06 '16 at 19:11

1 Answers1

1

Here's just one way

library(dplyr)
library(lubridate)
dat %>% 
  group_by(quarter=quarter(date, T)) %>% 
  filter(date==max(date)) %>% 
  ungroup %>% 
  select(-quarter)
# # A tibble: 7 x 2
#         date value
#       <date> <dbl>
# 1 2015-03-31    90
# 2 2015-06-30   181
# 3 2015-09-30   273
# 4 2015-12-31   365
# 5 2016-03-31   456
# 6 2016-06-30   547
# 7 2016-08-01   579
lukeA
  • 53,097
  • 5
  • 97
  • 100