blk.nearestMatch: Impute Values Using the Nearest Match

Description Usage Arguments Details Value Author(s) See Also


This function imputes the value of a quantity that would have been observed at a given time using the value that was observed nearest in time in a block-format data set.

The imputation can be done forwards of backwards in time, and can be constrained such that the value is only imputed if the observation occurred within a certain time window.


blk.nearestMatch(time, id, observ.x, observ.t, id2 = id, direction = c("forward", "backward", "both"), tol = Inf, fill = NA, na.rm = FALSE, diff.op = difftime.default)



A vector of times at which to impute values, in block-format with respect to id.


A valid block-format ID.


A vector of obseved values in block-format with respect to id2.


A vector of observation times corresponding to observ.x, in block-format with respect to id2.


Should value be carried forward in time, backward in time, or both?


A numeric vector of length 1 or 2. The time window or tolerance for matching. See details.


A value to use when no other value is appropriate.


Should missing value in observ.x be removed?


A function that subtracts one time from another.


For a given list of times, finds the observed values that are closest in time, searching either backward in time or forward in time. This is done for each block of time in a block-format data set.

For a given time, a match is declared if an observation occurs within a specific time window. The size of the time window is specified with the tolerance parameter tol. If tol has length 2 then the first number is the tolerance for matching in the “forward” direction and the second number is the tolerance for matching in the “backward” direction; if tol has length 1 then this is the tolerance for matching in both directions.

If two or more observations that are equidistant from a time value according to diff.op, then one of the values is selected arbitrarily. Forward matching value are selected in preference to backward matching values (when direction == "both").


A vector in block-format with respect to id containig the nearest match imputed values at times time from observ.x.


Ben Rich <>

See Also

benjaminrich/PCSmisc documentation built on May 24, 2019, 5:02 a.m.