approx_df {COINr}R Documentation

Interpolate time-indexed data frame


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, ...)



A data frame with all numeric columns


A time vector with length equal to nrow(Y), indexing the rows in Y.


A time vector of points to interpolate in Y. If NULL, will attempt to interpolate all points in Y (you may need to adjust the rule argument of stats::approx() here). Note that points not specified in tt_est will not be interpolated. tt_est does not need to be a subset of tt.


Further arguments to pass to stats::approx() other than x, y and xout.


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:

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)

# 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)

[Package COINr version 1.1.7 Index]