简体   繁体   中英

R: Include only business day observations in a data frame with observations for all days

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM