Helper function to match the year in the data with either the (1) exact; (2) most recent (past); (3) closest (past or future); or (4) soonest (future) year in another dataset. The column name within both datasets that represent the year must be specified. Dataset containing multiple years must be in the 'long' format.

matchyear(
  data,
  data_tomatch,
  match = c("exact", "closest", "recent", "soonest"),
  year = NULL
)

Arguments

data

Tabular object (e.g. data.frame, sf, tibble) containing the data.

data_tomatch

Tabular object (e.g. data.frame, sf, tibble) to match the data to.

match

Type of matching; either 'exact', 'closest', 'recent' or 'soonest'.

year

Specify column name for the year within both datasets. Columns in both datasets should be numeric.

Value

data with additional column year_match, representing the matching year of the other dataset.