Arne
Arne

Reputation: 167

Interpolation over time

In a dataframe, I have wind speed data measured four times a day, at 00:00, 06:00, 12:00 and 18:00 o'clock. To combine these with other data, I need to fill the time in between towards a resolution of 15 minutes. I would like to fill the gaps by simple interpolation.

The following example produces two corresponding sample dataframes. df1 and df2 need to be merged. In the resulting merged dataframe, the gap values between the 6-hourly values (where var == NA?) need to be filled by a simply mean interpolation. My problem is how to merge both and do the concrete interpolation between the given values.

First dataframe

Creation:

# create a corresponding sample data frame
df1 <- data.frame(
  date = seq.POSIXt(
    from = ISOdatetime(2015,10,1,0,0,0, tz = "GMT"),
    to = ISOdatetime(2015,10,14,23,59,0, tz= "GMT"),
    by = "6 hour"
  ),
  windspeed = abs(rnorm(14*4, 10, 4)) # abs() because windspeed shoud be positive
)

Resulting dataframe:

> # show the head of the dataframe
> head(df1)
                 date windspeed
1 2015-10-01 00:00:00 17.928217
2 2015-10-01 06:00:00 11.306025
3 2015-10-01 12:00:00  6.648131
4 2015-10-01 18:00:00 10.320146
5 2015-10-02 00:00:00  2.138559
6 2015-10-02 06:00:00  9.076344

Second dataframe

Creation:

# create a 2nd corresponding sample data frame
df2 <- data.frame(
  date = seq.POSIXt(
    from = ISOdatetime(2015,10,1,0,0,0, tz = "GMT"),
    to = ISOdatetime(2015,10,14,23,59,0, tz= "GMT"),
    by = "15 min"
  ),
  var = abs(rnorm(14*24*4, 300, 100))
)

Resulting dataframe:

> # show the head of the 2nd dataframe
> head(df2)
                 date      var
1 2015-10-01 00:00:00 198.2657
2 2015-10-01 00:15:00 472.9041
3 2015-10-01 00:30:00 605.8776
4 2015-10-01 00:45:00 429.0949
5 2015-10-01 01:00:00 400.2390
6 2015-10-01 01:15:00 317.1503

Upvotes: 1

Views: 758

Answers (1)

Derek Corcoran
Derek Corcoran

Reputation: 4102

This is a solution

First merge them to get using all = TRUE to get all values

df3 <- merge(df1, df2, all = TRUE)

Then use approx for Interpolation

df3$windspeed <- approx(x = df1$date, y = df1$windspeed, xout = df2$date)$y

The only problem there is that the las ones will be NA unless your last value of windspeed is there, but everything in between will be there

Upvotes: 1

Related Questions