match_ts_nearest: Find the position in one vector that is nearest in time to a...

View source: R/utils.R

match_ts_nearestR Documentation

Find the position in one vector that is nearest in time to a value in another dataframe

Description

This function is like match, but the aim is, for a given sequence of times (times), to find the positions in another sequence of times (lookup) that are nearest in time to those in the first sequence. In other words, for every time inputted, match_ts_nearest() finds the position in another sequence of times which is nearest in time to that time. This is useful if, for example, you have an existing dataframe to which you want to add the observations, held in another dataframe, that are nearest in time to observations in the first dataframe (i.e., nearest neighbour interpolation). This function uses data.table for fast matching, even with very large vectors.

Usage

match_ts_nearest(times, lookup)

Arguments

times

A vector of timestamps for which you want to identify the position of the nearest timestamp in another vector (lookup).

lookup

A vector of timestamps for which you will determine the position of the nearest timestamp to each time in times.

Details

If there are multiple matches, only the first is returned.

Value

For a sequence of times (times), the function returns a vector of the positions of the nearest times in another sequence (lookup).

Author(s)

Edward Lavender


edwardlavender/fvcom.tbx documentation built on Nov. 26, 2022, 10:28 p.m.