# ---------------------------------------------------------------------------
# Semi- and anti-joins: basic usage
# ---------------------------------------------------------------------------
# data frames
x <- data.table::fread(data.table = FALSE, input = "
country pop_m
Australia 27.2
Brazil 212.0
Chad 3.0
")
y <- data.table::fread(data.table = FALSE, input = "
country forest_pc
Brazil 59.1
Chad 3.2
Denmark 15.8
")
# full join with `indicate = TRUE` for comparison
fjoin_full(x, y, on = "country", indicate = TRUE)
fjoin_semi(x, y, on = "country")
fjoin_anti(x, y, on = "country")
fjoin_right_semi(x, y, on = "country")
fjoin_right_anti(x, y, on = "country")
# ---------------------------------------------------------------------------
# `mult.x` and `mult.y` support
# ---------------------------------------------------------------------------
# data frames
events <- data.table::fread(data.table = FALSE, input = "
event_id event_ts
1 10
2 20
3 40
")
reactions <- data.table::fread(data.table = FALSE, input = "
reaction_id reaction_ts
1 30
2 50
3 60
")
# ---------------------------------------------------------------------------
# for each event, the next reaction, provided there was no intervening event (1:1)
fjoin_full(
events,
reactions,
on = c("event_ts < reaction_ts"),
mult.x = "first",
mult.y = "last",
indicate = TRUE
)
fjoin_semi(
events,
reactions,
on = c("event_ts < reaction_ts"),
mult.x = "first",
mult.y = "last"
)
fjoin_anti(
events,
reactions,
on = c("event_ts < reaction_ts"),
mult.x = "first",
mult.y = "last"
)
# ---------------------------------------------------------------------------
# Natural join
# ---------------------------------------------------------------------------
fjoin_semi(x, y, on = NA)
fjoin_anti(x, y, on = NA)
# ---------------------------------------------------------------------------
# Mock join
# ---------------------------------------------------------------------------
fjoin_semi(on="id")
fjoin_semi(on=c("id", "date"))
fjoin_semi(on=c("id"), mult.y = "last")
Run the code above in your browser using DataLab