I've tried doing this using a combo of Dplyr filter and lag, but it isn't working. This stack overflow answer works for individual inputs, but not for an input dataframe as far as I can tell.
I have a dataframe of stock names, prices and dates. I would like to be able to input a second dataframe of stock names and dates and return those observations from the first dataframe, plus the N above, let's say N=1 for example. The observation dates are uneven so relying on it being N days beforehand won't work.
So for example if I have this data:
stock.data <- data.frame(
stock_name = c("Walmart","Walmart","Walmart","Target","Target","Target"),
price = c(100,101,102,201,202,203),
date = as.Date(c("2012-01-01", "2012-03-01", "2012-04-01", "2012-01-01",
"2012-03-01","2012-04-01"))
)
And in the other data frame, I have
other <- data.frame(
stock_name = c("Walmart", "Target"),
date = as.Date(c("2012-03-01", "2012-04-01"))
)
N <- 1
I would like to get the rows with prices of 100, 101, 202 and 203.
Hopefully this makes sense and I'm happy to answer further questions.