mt_join_time: Join values onto a move2 object by closest timestamp

View source: R/mt_join_time.R

mt_join_timeR Documentation

Join values onto a move2 object by closest timestamp

Description

Joins a move2 object with a data.frame, matching on track id and timestamp. Values can be matched with the closest overall timestamp, the closest timestamp before or after each timestamp, or by an exact match (a standard join).

The join will always use the mt_track_id_column() and mt_time_column() of the input move2 as the track id and timestamp, respectively. To join on different columns, set them beforehand with mt_set_track_id_column() and/or mt_set_time_column().

Usage

mt_join_time(
  x,
  y,
  ...,
  method = "closest",
  window = NULL,
  track_id_column_y = mt_track_id_column(x),
  time_column_y = mt_time_column(x),
  suffix_y = ".y",
  multiple = NULL,
  on_reused_y = "warn",
  na_matches = "never"
)

Arguments

x

A move2 object.

y

A data.frame containing columns with track id and timestamp information.

...

Additional join conditions passed to dplyr::join_by() that further restrict which records in y may match each record in x.

method

How to match timestamps during the join. Matches are only made within the same track and among y records that satisfy the conditions in ....

  • "closest" (default) matches each record in x to the record in y with the closest timestamp.

  • "closest_before" matches each record in x to the record in y with the closest timestamp that is at or before the timestamp in x.

  • "closest_after" matches each record in x to the record in y with the closest timestamp that is at or after the timestamp in x.

  • "exact" matches records that have the exact same timestamp.

window

Maximum time difference allowed between a record in x and its matched record in y. If no timestamps in y fall within this window for a given x, then no values are joined to that record. By default, no window restriction is applied.

If mt_time(x) is a date or datetime, window should be provided with time units (e.g. units::set_units(1, "s")). If mt_time(x) is numeric, window should also be numeric.

track_id_column_y

Name of the column containing the track IDs in y. By default, uses the same name as the track id column in x.

time_column_y

Name of the column containing the timestamps in y. By default, uses the same name as the time column in x.

suffix_y

Suffix appended to column names from y to disambiguate them from column names that also exist in x, if needed (for instance, if x and y share the same track id and timestamp column names).

multiple

How to handle rows in x that have multiple matches in y (that is, when multiple y records are equidistant to a given x record).

  • "all" (default) keeps all matches. x records with multiple matches are duplicated for each matching y record. No data is lost. This option allows you to decide later about which records to use (for instance, based on data quality). allows you to make an informed decisions what records to use based on data quality later. Setting the argument explicitly prevents being warned about duplications of records.

  • "first" keeps only the match that comes first in y's row order.

  • "last" keeps only the match that comes last in y's row order.

Note that "first" and "last" determine which matches to keep using the row order of y, not the time order. To have these options respect time order, first sort y by time.

on_reused_y

What to do when a single record in y is matched to more than one record in x. Note that reusing y values may violate certain assumptions in later analyses.

  • "warn" (default) shows a warning.

  • "error" throws an error. Use this to ensure that every y record is present at most once in the output.

  • "ignore" includes reused records without a warning. Use this to suppress this warning if you expect reused y records.

na_matches

Indicator for whether NA values should be considered to match. "never" (default) refuses to match records with NA values in any of the join columns. "na" matches missing values to each other. Note that setting na_matches = "na" can lead to a large increase in the number of rows in the output if many columns contain NA records.

Details

Reused and duplicated records

Note that y values may be reused during the join. That is, if the same y record is nearest to multiple x records, then it will be matched to each of those x records. You can mitigate this behavior by providing a window to match only nearby records, but this does not fully prevent reuse from occurring. To identify which values are reused, add a row identifier column to y before joining and look for duplicated row IDs in the joined output.

By default, a warning is shown when y records are reused in the output. If you expect y records to be reused, you can set on_reused_y = "ignore". To stop with an error instead, set on_reused_y = "error".

Similarly, if a record in x is equally close to multiple records in y, that record will appear multiple times in the joined output. This happens when several records in y share the same timestamp or when the nearest y records before and after an x record are the same distance away (for method = "closest"). To instead ensure that only a single record is matched in these cases, use the multiple argument to specify a selection criterion.

Type coercion of join key columns

If any of the columns matched by equality have different object types in x and y, the output column will be coerced to the common type of x and y. For example, if the track id in x is a factor column and the track id in y is a character column, the output column will be character.

Value

A move2 containing all rows in x with additional columns and values from y attached. By default, rows in x that match multiple rows in y will appear multiple times in the output.

Author(s)

Finn Roberts

See Also

dplyr::left_join()

Examples

x <- mt_sim_brownian_motion(t = 1:5, tracks = c("a", "b"))
y <- data.frame(
  track = rep(c("a", "b"), each = 3),
  time = c(1.4, 2.7, 4.5, 0.5, 3, 4.2),
  sensor_value = 1:6
)

# For each x record, attach the temporally closest y record
# Note that for multiple records in x the same record in y is the closest one
mt_join_time(x, y)

# Restrict to only consider records before or after the `x` timestamp
mt_join_time(x, y, method = "closest_before")

mt_join_time(x, y, method = "closest_after")

# or to match on exact timestamp matches only
mt_join_time(x, y, method = "exact")

# By default the closest match at any time gap is used. To only match if the
# `y` record is within a certain time difference, use `window`:
mt_join_time(x, y, window = 0.5)

# `mt_join_time()` always matches using `x`'s designated track id and time
# column. If the `y` track and timestamps use different column names, you
# can provide them:
colnames(y) <- c("track_id", "timestamp", "sensor_value")
mt_join_time(
  x,
  y,
  track_id_column_y = "track_id",
  time_column_y = "timestamp"
)

# If any `x` records are equally close to multiple `y` records, all matches
# are retained
y <- data.frame(
  track = c("a", "a", "b", "b"),
  time = c(2.5, 1.5, 3.5, 4.5),
  sensor_value = 1:4
)
mt_join_time(x, y)

# Set `multiple` to select a single match in these cases
mt_join_time(x, y, multiple = "first")

# `multiple` matches by `y` row order. If you want to match by time order,
# you must first sort `y` by time
y <- y[order(y$track, y$time), ]
mt_join_time(x, y, multiple = "first")

# You can provide additional join criteria to further restrict the matched
# records. For instance, you may have data for multiple sensors:
x$sensor <- c("acc", rep("gps", 4), "acc", rep("gps", 4))

# But other data may only be associated with a particular sensor:
y <- data.frame(
  track = c("a", "a", "a", "b", "b"),
  time = c(1.6, 3.6, 5.6, 2.6, 4.6),
  sensor = "gps",
  ndvi = c(0.62, 0.55, 0.44, 0.35, 0.42)
)

# By default every record in x gets a match, including accelerometer records:
mt_join_time(x, y)

# Additional match criteria ensure only the relevant records are considered,
# while retaining all `x` records:
mt_join_time(x, y, sensor == sensor)

move2 documentation built on Dec. 2, 2026, 1:08 a.m.