I'm trying to backfill a fully outerjoined table with nearest preceding column data.
The data frame I have looks like..
(No rows have both sides as NA and the table is sorted by date).
date X Y
2012-07-05 00:01:19 0.0122 NA
2012-07-05 03:19:34 0.0121 NA
2012-07-05 03:19:56 0.0121 0.027
2012-07-05 03:20:31 0.0121 NA
2012-07-05 04:19:56 0.0121 0.028
2012-07-05 04:20:31 0.0121 NA
2012-07-05 04:20:50 0.0121 NA
2012-07-05 04:22:29 0.0121 0.027
2012-07-05 04:24:37 0.0121 NA
2012-07-05 20:48:45 0.0121 NA
2012-07-05 23:02:34 NA 0.029
2012-07-05 23:30:45 NA 0.029
with this, I'm looking to..
- leave the non-data missing rows as it is.
- If either one side is missing (NA), then fill it with the "nearest preceding" row which has valid opposite side's value.
And so as the result, I would like to have the table looking like...
date X Y
2012-07-05 00:01:19 0.0122 NA
2012-07-05 03:19:34 0.0121 NA
2012-07-05 03:19:56 0.0121 0.027
2012-07-05 03:20:31 0.0121 0.027
2012-07-05 04:19:56 0.0121 0.028
2012-07-05 04:20:31 0.0121 0.028
2012-07-05 04:20:50 0.0121 0.028
2012-07-05 04:22:29 0.0121 0.027
2012-07-05 04:24:37 0.0121 0.027
2012-07-05 20:48:45 0.0121 0.027
2012-07-05 23:02:34 0.0121 0.029
2012-07-05 23:30:45 0.0121 0.029
What kind of R commands can I use to achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…