approx_dt: Approximate missing values in a data.table.

View source: R/approx.R

approx_dtR Documentation

Approximate missing values in a data.table.

Description

Similar to, but not quite like, 'stats::approx'. Does only support constant extrapolation and linear interpolation. The resulting 'data.table' only contains the range provided by 'xdata' along 'xcol'. Without extrapolation, 'xcol' in the resulting 'data.table' may not cover the range given by 'xdata'.

Usage

approx_dt(
  dt,
  xdata,
  xcol,
  ycol,
  idxcols = NULL,
  keepna = FALSE,
  extrapolate = FALSE
)

Arguments

dt

a data.table.

xdata

the range to interpolate to. This is the range the result will have along the dimension 'xcol'.

xcol

name of the column for interpolation.

ycol

name of the column that contains the value to be interpolated.

idxcols

columns that identify a row (besides xcol), i.e., the remaining index dimensions.

keepna

keep NA values for rows that can not be interpolated (since they are outside of [min(xcol), max(xcol)]), default is FALSE.

extrapolate

use the closest values to fill 'ycol' outside of the interpolation domain, default is FALSE. This will also work if there is only one value along 'ycol', i.e., no interpolation is taking place.

Value

a data.table with the range given by 'xdata' along 'xcol'. Columns not given in 'idxcols' will be kept but NAs will appear on extrapolated and interpolated rows.

Examples

dt <- as.data.table(ChickWeight)
## delete all values but 1
dt[Chick == 1 & Time > 0, weight := NA]
## delete all values but 2
dt[Chick == 2 & Time > 2, weight := NA]

## extrapolation from 1 value
approx_dt(dt, 0:21, "Time", "weight", idxcols=c("Chick", "Diet"), extrapolate = TRUE)[Chick == 1]
## extrapolation and interpolation
approx_dt(dt, 0:21, "Time", "weight", idxcols=c("Chick", "Diet"), extrapolate = TRUE)[Chick == 2]
## column not in idxcols
approx_dt(dt, 0:21, "Time", "weight", idxcols="Chick", extrapolate = TRUE)[Chick == 2]

dt <- as.data.table(ChickWeight)
## interpolation only
approx_dt(dt, 0:21, "Time", "weight", idxcols=c("Chick", "Diet"))[Chick == 2]

pik-piam/rmndt documentation built on April 21, 2024, 4:31 a.m.