0

I have a data frame with a date column and two columns of observations. I want to create a new data frame where only the observations that are from business days are included.

I tried df=df[which(weekdays.Date(as.Date.dates(df$Date, format = "%Y-%m-%d")) %in% c('Rmetrics/NYSE')), ] to only include rows that contain dates that are in the NYSE calendar. This did not work.

axor93
  • 1
  • Where and how is this `Rmetrics/NYSE` thing defined? – r2evans Nov 06 '19 at 14:14
  • 1
    axor93, `weekday` returns `"Monday", "Tuesday", ...` (locale-dependent), and you are comparing that vector against a static string vector with one element, `"Rmetrics/NYSE"` (which is just a string). This will never return any rows. Is this supposed to be something from the archived and empty [`Rmetrics`](https://github.com/cran/Rmetrics) package? – r2evans Nov 06 '19 at 14:24
  • I understand that `Rmetrics/NYSE` does not work with `weekday`, but I was just trying to explain what I am aiming for. I'll try to explain my problem again: I know that e.g. `df=df[which(weekdays.Date(as.Date.dates(df$Date, format = "%Y-%m-%d")) %in% c("Monday","Tuesday")` removes all rows that are not on Monday or Tuesday. I would like to remove all rows that are not trading days in the NYSE. That is why I just replaced `"Monday","Tuesday"`with `"Rmetrics/NYSE"` in the example to show what I want to do. – axor93 Nov 07 '19 at 08:20

1 Answers1

0
library(chron)
library(lubridate)
library(timeDate)

df$Date <- ymd(df$Date)
df[!is.weekend(df$Date) & !(df$Date %in% holidayNYSE(2008:2010)),]

You can use timeDate::holidayNYSE() to get a holiday calendar for the New York Stock Exchange.

> library(timeDate)
> holidayNYSE()
NewYork
[1] [2019-01-01] [2019-01-21] [2019-02-18] [2019-04-19] [2019-05-27] [2019-07-04] [2019-09-02]
[8] [2019-11-28] [2019-12-25]
> holidayNYSE(2008:2010)
NewYork
 [1] [2008-01-01] [2008-01-21] [2008-02-18] [2008-03-21] [2008-05-26] [2008-07-04]
 [7] [2008-09-01] [2008-11-27] [2008-12-25] [2009-01-01] [2009-01-19] [2009-02-16]
[13] [2009-04-10] [2009-05-25] [2009-07-03] [2009-09-07] [2009-11-26] [2009-12-25]
[19] [2010-01-01] [2010-01-18] [2010-02-15] [2010-04-02] [2010-05-31] [2010-07-05]
[25] [2010-09-06] [2010-11-25] [2010-12-24]
Tianyu Z
  • 121
  • 1
  • 4
  • Unfortunately this does not answer my problem. This does the same job as `df=df[which(weekdays.Date(as.Date(df$Date, format = "%Y-%m-%d")) %in% c('Monday','Tuesday', 'Wednesday', 'Thursday', 'Friday')), ]`. It removes all rows that are not weekdays. However, this is not the same thing as business (trading) days. In addition to removing weekends, I want to remove holidays and other non-trading days as well. – axor93 Nov 07 '19 at 08:25