I want to create a function in R that solves the following problem. I have a sample of returns (call this vector x
), each one with a corresponding date (call this vector y
). Since we have returns, the date vector y
has only "trading days" (i.e. from Monday to Friday). I want to create a function that does the following:
The function has two inputs: a date and an integer (positive).
R searches in the date vector
y
for the date entered by the user.Say the integer entered by the user was
m
;m > 0
. Now, R takes the specified date andm
dates BEFORE that one. (Ifm < 0
, R must tell you there's an error.)R subsets the date vector
y
based onm
and also the return vectorx
.
For a sake of an example, let's say I have 5 dates in "%m/%d/%Y"
format.
01/01/2016 -2%
01/04/2016 +3%
01/05/2016 +1%
01/06/2016 -5%
01/07/2016 +3%
Now, if I enter into the function: f(01/06/2016, 2)
, then I would be left with:
01/05/2016 +1%
01/06/2016 -5%
I know this is quite specific, but it would teach me a lot about subsetting. (I haven't been able to find anything similar to this.)