approx_df {COINr} | R Documentation |
Given a numeric data frame Y
with rows indexed by a time vector tt
, interpolates at time values
specified by the vector tt_est
. If tt_est
is not in tt
, will create new rows in the data frame
corresponding to these interpolated points.
approx_df(Y, tt, tt_est = NULL, ...)
Y |
A data frame with all numeric columns |
tt |
A time vector with length equal to |
tt_est |
A time vector of points to interpolate in |
... |
Further arguments to pass to |
This is a wrapper for stats::approx()
, with some differences. In the first place, stats::approx()
is
applied to each column of Y
, using tt
each time as the corresponding time vector indexing Y
. Interpolated
values are generated at points specified in tt_est
but these are appended to the existing data (whereas
stats::approx()
will only return the interpolated points and nothing else). Further arguments to
stats::approx()
can be passed using the ...
argument.
A list with:
.$tt
the vector of time points, including time values of interpolated points
.$Y
the corresponding interpolated data frame
Both outputs are sorted by tt
.
# a time vector
tt <- 2011:2020
# two random vectors with some missing values
y1 <- runif(10)
y2 <- runif(10)
y1[2] <- y1[5] <- NA
y2[3] <- y2[5] <- NA
# make into df
Y <- data.frame(y1, y2)
# interpolate for time = 2012
Y_int <- approx_df(Y, tt, 2012)
Y_int$Y
# notice Y_int$y2 is unchanged since at 2012 it did not have NA value
stopifnot(identical(Y_int$Y$y2, y2))
# interpolate at value not in tt
approx_df(Y, tt, 2015.5)