I have 2-time series data with different time stamps and a different number of data points.
First data frame is:
Time Power_kW
10/9/2017 1:14:12 0.185
10/9/2017 1:14:53 0.182
10/9/2017 1:15:13 0.184
10/9/2017 1:15:53 0.175
. .
. .
10/9/2017 1:44:37 0.175
. .
. .
10/9/2017 2:13:38 0.181
. .
. .
10/9/2017 2:24:40 0.179
Second dataframe is:
Local Time Value
10/9/2017 1:13:01 0
10/9/2017 1:42:10 1
10/9/2017 2:11:58 0
10/9/2017 2:23:30 1
The second data frame values indicate the value in between 2 dates mentioned. It means the value of '0' should be propagated from 10/9/2017 1:13:01 0 to 10/9/2017 1:42:10. and the value 1 from the date of 10/9/2017 1:42:10 up to 10/9/2017 2:23:30 and so on.
I wish to have the same number of data points as the first data frame by merging these two data frames columns values. I have many similar data frames as the Second dataframe which all will help me making correlations with the respect to the time.
I am trying to get the following result after merging:
Time Power_kW Value
10/9/2017 1:14:12 0.185 0
10/9/2017 1:14:53 0.182 0
10/9/2017 1:15:13 0.184 0
10/9/2017 1:15:53 0.175 0
. .
. .
10/9/2017 1:44:37 0.175 1
10/9/2017 1:45:47 0.176 1
. .
10/9/2017 2:13:38 0.181 0
. .
. .
10/9/2017 2:24:40 0.179 1
(I have added some points in the result to represent how the whole data should look like.)
I did refer to these threads and their purpose are not propagating through.
R: merge two irregular time series
merge two time series with different time granularities
Can anyone please throw me a light?
Try a rolling join using R's
data.table
package, which is explained nicely in this blog:Assume the inputs given in the Note at the end. Convert the data frames to zoo and run
na.locf
to fill in the NA values. Finally select the elements corresponding to the first table and optionally convert back to data frame:giving:
Note: We assume the following inputs. Note that we assume POSIXct date times.