Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get every third Friday of every third month in R

Tags:

r

Trying to get the date for the third friday of every three months in the year (March, June, Sept, Dec) from 2018 to 2020. However, my current code just gives me every 3rd friday. So b's output will actually give me two Friday dates in June 2018. See code below:

allDates = seq(from = as.Date("2018-01-01"), to = as.Date("2020-07-01"), by = "days")
df = data.frame(allDates)
df$Date = weekdays(df$allDates)
Day = data.frame()
fridays = data.frame()

a = df[month(allDates)%in%c(2,5,8,11) & weekday(allDates)==5,]
b = a[seq(from = 3, to = nrow(a), by = 3),]

How would i then assign different variables to specific dates? for example, x = third friday of March and Sept. and y = third friday of March, June, Sept, and Dec?

like image 438
worldCurrencies Avatar asked Oct 26 '25 02:10

worldCurrencies


2 Answers

The following base R code outputs the 3rd Fridays of the required months. The output month names are in my current locale but the code itself does not depend on it.

friday3 <- function(start.year, end.year){
  d <- seq(ISOdate(start.year - 1, 12, 1), ISOdate(end.year, 12, 1), by = "3 month")[-1]
  d <- as.Date(d)
  res <- lapply(d, function(x){
    s <- seq(x, by = "day", length.out = 28)
    i <- format(s, "%u") == "5"
    s[i][3]
  })
  
  res <- Reduce(c, res)
  data.frame(Month = format(d, "%Y-%B"), Day = res)
}

My locale is not English, so I have to set an English language locale to test it, but the code itself does not depend on locale.

#ol <- Sys.getlocale("LC_TIME")
#Sys.setlocale("LC_TIME", "en_US.UTF-8")

friday3(2018, 2020)
#            Month        Day
#1      2018-March 2018-03-16
#2       2018-June 2018-06-15
#3  2018-September 2018-09-21
#4   2018-December 2018-12-21
#5      2019-March 2019-03-15
#6       2019-June 2019-06-21
#7  2019-September 2019-09-20
#8   2019-December 2019-12-20
#9      2020-March 2020-03-20
#10      2020-June 2020-06-19
#11 2020-September 2020-09-18
#12  2020-December 2020-12-18

And back to mine.

#Sys.setlocale("LC_TIME", ol)
like image 99
Rui Barradas Avatar answered Oct 28 '25 14:10

Rui Barradas


Are you looking for something like this? (data is based on the three first lines of your code).

The three important parameters are the month and date you want to filter (c(3,6,9,12) and 'Friday' in filter(month(allDates)%in%c(3,6,9,12) & Date == 'Friday')) and the nth position of the day (3 in summarise(col=nth(allDates,3))).

library(lubridate)
library(dplyr)

df %>% 
  filter(month(allDates)%in%c(3,6,9,12) & Date == 'Friday') %>% 
  mutate(month=month(allDates),
         year=year(allDates)) %>% 
  group_by(year,month) %>% 
  summarise(col=nth(allDates,3))

# A tibble: 10 x 3
# Groups:   year [3]
    year month col       
   <dbl> <dbl> <date>    
 1  2018     3 2018-03-16
 2  2018     6 2018-06-15
 3  2018     9 2018-09-21
 4  2018    12 2018-12-21
 5  2019     3 2019-03-15
 6  2019     6 2019-06-21
 7  2019     9 2019-09-20
 8  2019    12 2019-12-20
 9  2020     3 2020-03-20
10  2020     6 2020-06-19
like image 30
Maël Avatar answered Oct 28 '25 15:10

Maël



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!