| mt_join_time | R Documentation |
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().
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"
)
x |
A |
y |
A data.frame containing columns with track id and timestamp information. |
... |
Additional join conditions passed to |
method |
How to match timestamps during the join. Matches are only made
within the same track and among
|
window |
Maximum time difference
allowed between a record in If |
track_id_column_y |
Name of the column containing the track IDs in |
time_column_y |
Name of the column containing the timestamps in |
suffix_y |
Suffix appended to column names from |
multiple |
How to handle rows in
Note that |
on_reused_y |
What to do when a single record in
|
na_matches |
Indicator for whether |
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.
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.
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.
Finn Roberts
dplyr::left_join()
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.