How could two dataframes be "resized" or cut by the value of one of their columns? My objective is to operate with them, that is to make the difference of the values for the coinciding dates.
I do not know if it could be done in an efficient way without having to create variables that store the two dataframes and calculate the error.
The Data: two dataframes of 2 columns (time and value) with different lengths, one starts later than the other and ends after the second.
df1
time value
1 2001-02-01 107
2 2001-04-01 122
3 2001-06-01 123
4 2001-08-01 101
5 2001-10-01 130
6 2001-12-01 116
7 2002-02-01 108
8 2002-04-01 154
9 2002-06-01 146
10 2002-08-01 111
11 2002-10-01 110
12 2002-12-01 133
df2
time value
3 2001-06-01 223
4 2001-08-01 131
5 2001-10-01 134
6 2001-12-01 16
7 2002-02-01 228
dfR Desired result:
3 2001-06-01 100
4 2001-08-01 30
5 2001-10-01 4
6 2001-12-01 -100
7 2002-02-01 120
Edit: # a possible solution is to make a merge for "time" and then operate ...
dfR<- merge(df1,df2, by = c("time"))
Any other better solution?