I have a dataframe containing daily prices of a stock exchange for several years with their respective dates. I would like to extract the last 3 observations in a month and the first 5 observations of the following month, for every month, and store it in a new dataframe.
In addition to dates (formated as "%Y-%m-%d") I have a column with a counter for every trading day pr month. Example data looks like this:
df$date <- as.Date(c("2017-03-25","2017-03-26","2017-03-27","2017-03-29","2017-03-30",
"2017-03-31","2017-04-03","2017-04-04","2017-04-05","2017-04-06",
"2017-04-07","2017-04-08","2017-04-09"))
df$DayofMonth <- c(18,19,20,21,22,23,1,2,3,4,5,6,7)
df$price <- (100, 100.53, 101.3 ,100.94, 101.42, 101.40, 101.85, 102, 101.9, 102, 102.31, 102.1, 102.23)
And now I want to extract the last 3 observations in March and the first 5 observations in April (and then the last 3 observations in April and the first 5 in May etc, including all columns of the respective rows) and store it in a new dataframe. The only question is how do I do this?
Thanks for helping out!
First idea:
date <- c("2017-03-25","2017-03-26","2017-03-27","2017-03-29","2017-03-30",
"2017-03-31","2017-04-03","2017-04-04","2017-04-05","2017-04-06",
"2017-04-07","2017-04-08","2017-04-09")
df <- data.table(Date = date)
df[,YearMonth:=str_sub(Date,1,7)]
df[, DayofMonth := seq(.N), by = YearMonth]
first <- df[, .SD[1:ifelse(.N < 5, .N, 5)], by = YearMonth] #first trading days each month
last <- df[, .SD[(ifelse((.N-2) < 0, 0, (.N-2))):.N], by = YearMonth] #last trading days each month
final <- rbind(first, last)
setorder(final, Date)
# be aware that it leads to duplicates for a month if it has less than 8 trading days,
# to resolve that use unique()
final <- unique(final)