Aquarius
Aquarius

Reputation: 262

Removing rows by date in R - Financial data

I am currently working on my master's thesis. In my dataset I have daily observation of 5413 companies for 15 years. I need to remove non trading days i.e. holidays at London Stock Exchange such as Christmas, New years etc . I have referred to 'Removing rows containing specific dates in R' but couldn't find solution to my prblem My data set has closing prices of stocks and looks as follows (has the same data format)

Date          A       B
31/12/1999  NA        NA
03/01/2000  NA        NA
04/01/2000  79.5      325
05/01/2000  79.5      322.5
06/01/2000  NA       NA   
07/01/2000   79.5   327.5

And for example I want to remove dates 31/12/1999 and 06/01/2000 so that my data looks like

Date        A         B
03/01/2000  NA        NA
04/01/2000  79.5    325
05/01/2000  79.5    322.5  
07/01/2000  79.5    327.5

I'm new to R but this stackoverflow has been very helpful. I thank you in advance

Upvotes: 1

Views: 947

Answers (1)

mpalanco
mpalanco

Reputation: 13580

Assuming that your column Date is a character vector, you could just modify the answer provided by user3949008 in the comments, specifying inside holidayLONDON the years you need:

df$Date <- as.Date(df$Date,"%d/%m/%Y")
df[!df$Date %in% as.Date(holidayLONDON(1999:2000)), ] 

Output:

        Date    A     B
1 1999-12-31   NA    NA
3 2000-01-04 79.5 325.0
4 2000-01-05 79.5 322.5
5 2000-01-06   NA    NA
6 2000-01-07 79.5 327.5

Data:

df <- structure(list(Date = c("31/12/1999", "03/01/2000", "04/01/2000", 
"05/01/2000", "06/01/2000", "07/01/2000"), A = c(NA, NA, 79.5, 
79.5, NA, 79.5), B = c(NA, NA, 325, 322.5, NA, 327.5)), .Names = c("Date", 
"A", "B"), class = "data.frame", row.names = c(NA, -6L))

Upvotes: 1

Related Questions