Interpolate to dimensions missing in a data.table, between dimensions which exist. Uses linear interpolation.

interpolate(dt, id_cols, interpolate_col, value_col, interpolate_vals, ...)

Arguments

dt

[data.table()]
Data you would like to interpolate. Can either have NAs or be missing rows (implicit NAs).

id_cols

[characher()]
Columns which uniquely identify rows of dt. Interpolation will be done separately by group on id_cols excluding interpolate_col.

interpolate_col

[characher(1)]
Name of column of dt which is a numeric variable defining the groups you would like to interpolate for.

value_col

[characher(1)]
Name of a column of dt which is a numeric variable defining the values you would like to interpolate.

interpolate_vals

[numeric(1)]
The values of interpolate_col for which you would like to solve for an interpolated value of value_col.

...

Other arguments to be passed to stats::approx.

Value

[data.table()] dt with added rows for interpolated values.

Details

This function uses stats::approx to solve a linear interpolation. Values outside the bounds of known data will be returned as NA. Use extrapolate() to get values outside of the bounds of the data. Consider log transforming your data prior to interpolation if appropriate.

Examples

dt <- data.table::data.table( group = c(rep("a", 5), rep("b", 5)), x = c(1, 3, 4, 5, 10, 1, 2, 6, 8, 10), y = c(10, 30, 40, 50, 100, 10, 20, 60, 80, 100) ) dt <- interpolate(dt, id_cols = c("group", "x"), interpolate_col = "x", value_col = "y", interpolate_vals = c(1:10))