I have data like the following:
data <- tibble(time = c(ymd_hms("2019-11-01 09:33:00"),
ymd_hms("2019-11-01 09:35:00"),
ymd_hms("2019-11-01 09:40:00"),
ymd_hms("2019-11-01 09:52:00")),
data = c(1250, 900, 4000, 9000))
data
## A tibble: 4 x 2
# time data
# <dttm> <dbl>
# 1 2019-11-01 09:33:00 1250
# 2 2019-11-01 09:35:00 900
# 3 2019-11-01 09:40:00 4000
# 4 2019-11-01 09:52:00 9000
I want the data
column to be resampled for every minute between the first and last observation, and I want the value of data
to become the next non NAN value divided by 1 + the number of nan values from the prior non-nan value to the next non-nan value (i.e. the value of data
is "spread" back minutely from its given sample point to the prior given sample point).
In this case for instance, I would expect the following
> result
# A tibble: 20 x 2
time data
<dttm> <dbl>
1 2019-11-01 09:33:00 1250
2 2019-11-01 09:34:00 450
3 2019-11-01 09:35:00 450
4 2019-11-01 09:36:00 800
5 2019-11-01 09:37:00 800
6 2019-11-01 09:38:00 800
7 2019-11-01 09:39:00 800
8 2019-11-01 09:40:00 800
9 2019-11-01 09:41:00 750
10 2019-11-01 09:42:00 750
11 2019-11-01 09:43:00 750
12 2019-11-01 09:44:00 750
13 2019-11-01 09:45:00 750
14 2019-11-01 09:46:00 750
15 2019-11-01 09:47:00 750
16 2019-11-01 09:48:00 750
17 2019-11-01 09:49:00 750
18 2019-11-01 09:50:00 750
19 2019-11-01 09:51:00 750
20 2019-11-01 09:52:00 750
How can I do this?
I see in Zoo how to use na.locf
to do nearly what I want, but I don't see how to incorporate this "spreading" of the data instead of just filling with last value or doing linear interpolation.
I've also tried using xts
and merging two series (one of which is the irregular dates) with some custom logic but this is proving challenging for me.