I have two dataframes. Dataframe data
one has two columns: one contains ymd
dates, and the other values:
date value
1 2009-10-23 1100
2 2009-05-01 5000
3 2010-01-13 3050
4 2010-07-24 2700
5 2009-06-16 2600
My second dataframe (named factors
) also has two columns: another ymd
date, and a coefficient. Here, for each month of each year, I always have two specific dates: the 1st and 15th of each month. This is how the data frame looks (I only added some dates on this minimal example, but there shouldn't be any 'jumps': I have continued data in a 10-year period):
date coeff
1 2009-05-01 2.00
2 2009-05-15 3.00
3 2009-06-01 2.50
4 2009-06-15 4.00
5 2009-10-01 3.65
6 2009-10-15 4.80
7 2010-01-01 2.40
8 2010-01-15 1.90
9 2010-07-01 5.20
10 2010-07-15 4.30
The dataframes are ready to use on this fiddle: http://rextester.com/MOIY96065
My problem
I need to create a new column in dataframe 1 (named data
) where this column is data$value / factors$coeff
following a condition: it must use the coeff
with the previous closest date
value.
For example: date$value[1]
should be divided by factors$coeff[6]
(the value on October 15th), but date$value[2]
should be divided by factors$coeff[1]
(the value on May 1st).
My factors
dataframe is ordered by date. I've been using lubridate
to parse the dates from string type, but I don't know how can I make this work.