I apologize if this is redundant, but I have tried to look for solutions, and have not found anything that appears to be the answer to my question. So, I have time series data for a bunch of variables. Some variables (continuous) were recorded at 1000hz, and the other variables (categorical) is at 500hz. I want to align them in R, such that the categorical variables have the same number of rows as the continuous variables. In other words, I would like to merge the two dataframes and fill in the gaps automatically for the categorical variables, so that my data, which looks like this:
| t_emg | CF_01 | CF_02 |
|---|---|---|
| 0 | -0.01796875 | 0.01234375 |
| 0.001 | 0.001875 | 0.05125 |
| 0.002 | 0.00828125 | -0.04140625 |
| 0.003 | -0.0125 | -0.005625 |
| 0.004 | -0.00765625 | 0.00078125 |
| 0.005 | -0.01 | 0.02234375 |
| 0.006 | 0.01515625 | -0.01296875 |
| 0.007 | -0.01375 | -0.070625 |
| 0.008 | 0.0096875 | 0.1534375 |
| 0.009 | -0.05203125 | 0.02984375 |
| 0.01 | 0.1234375 | 0.00765625 |
| 0.011 | -0.0596875 | 0.005625 |
| 0.012 | -0.09296875 | -0.001875 |
| 0.013 | -0.0834375 | -0.1128125 |
| 0.014 | -0.009375 | 0.0090625 |
| 0.015 | -0.00609375 | 0.02 |
| 0.016 | 0.00890625 | 0.0209375 |
| 0.017 | 0.02671875 | 0.00375 |
| 0.018 | -0.001875 | -0.0003125 |
| 0.019 | -0.00171875 | 0.00171875 |
and this:
| t_kinematics | Phase | stride_stop |
|---|---|---|
| 0 | swing | A |
| 0.002 | swing | A |
| 0.004 | swing | A |
| 0.006 | swing | A |
| 0.008 | swing | A |
| 0.01 | swing | A |
| 0.012 | swing | A |
| 0.014 | stance | A |
| 0.016 | stance | A |
| 0.018 | stance | A |
will end up looking like this:
| t_emg | CF_01 | CF_02 | t_kinematics | Phase | stride_stop |
|---|---|---|---|---|---|
| 0 | -0.01796875 | 0.01234375 | 0 | swing | A |
| 0.001 | 0.001875 | 0.05125 | 0.001 | swing | A |
| 0.002 | 0.00828125 | -0.04140625 | 0.002 | swing | A |
| 0.003 | -0.0125 | -0.005625 | 0.003 | swing | A |
| 0.004 | -0.00765625 | 0.00078125 | 0.004 | swing | A |
| 0.005 | -0.01 | 0.02234375 | 0.005 | swing | A |
| 0.006 | 0.01515625 | -0.01296875 | 0.006 | swing | A |
| 0.007 | -0.01375 | -0.070625 | 0.007 | swing | A |
| 0.008 | 0.0096875 | 0.1534375 | 0.008 | swing | A |
| 0.009 | -0.05203125 | 0.02984375 | 0.009 | swing | A |
| 0.01 | 0.1234375 | 0.00765625 | 0.01 | swing | A |
| 0.011 | -0.0596875 | 0.005625 | 0.011 | swing | A |
| 0.012 | -0.09296875 | -0.001875 | 0.012 | swing | A |
| 0.013 | -0.0834375 | -0.1128125 | 0.013 | swing | A |
| 0.014 | -0.009375 | 0.0090625 | 0.014 | stance | A |
| 0.015 | -0.00609375 | 0.02 | 0.015 | stance | A |
| 0.016 | 0.00890625 | 0.0209375 | 0.016 | stance | A |
| 0.017 | 0.02671875 | 0.00375 | 0.017 | stance | A |
| 0.018 | -0.001875 | -0.0003125 | 0.018 | stance | A |
| 0.019 | -0.00171875 | 0.00171875 | 0.019 | stance | A |
Additionally, there are many NAs in the categorical data, and I would like those to be retained. All the information that I can find is more geared towards imbalanced data or imputation of missing values. It is also not really prediction, as I am really just interested in filling in the temporal gaps for the categorical variables.
From your single-frame, we really have two 3-column frames.
Using this, we'll use
findIntervalto map the other columns to the time interval.If you really need
t_kinematics, then just copy it over fromt_emg:We could also do a range-join:
The
1e-9is to force thebetweencomparison to be right-open; may not be required with your data, I wanted to be certain.