nest_join {dplyr} | R Documentation |
Nest join
Description
A nest join leaves x
almost unchanged, except that it adds a new
list-column, where each element contains the rows from y
that match the
corresponding row in x
.
Usage
nest_join(x, y, by = NULL, copy = FALSE, keep = NULL, name = NULL, ...)
## S3 method for class 'data.frame'
nest_join(
x,
y,
by = NULL,
copy = FALSE,
keep = NULL,
name = NULL,
...,
na_matches = c("na", "never"),
unmatched = "drop"
)
Arguments
x , y |
A pair of data frames, data frame extensions (e.g. a tibble), or lazy data frames (e.g. from dbplyr or dtplyr). See Methods, below, for more details. |
by |
A join specification created with If To join on different variables between To join by multiple variables, use a
For simple equality joins, you can alternatively specify a character vector
of variable names to join by. For example, To perform a cross-join, generating all combinations of |
copy |
If |
keep |
Should the new list-column contain join keys? The default will preserve the join keys for inequality joins. |
name |
The name of the list-column created by the join. If |
... |
Other parameters passed onto methods. |
na_matches |
Should two |
unmatched |
How should unmatched keys that would result in dropped rows be handled?
|
Value
The output:
Is same type as
x
(including having the same groups).Has exactly the same number of rows as
x
.Contains all the columns of
x
in the same order with the same values. They are only modified (slightly) ifkeep = FALSE
, when columns listed inby
will be coerced to their common type acrossx
andy
.Gains one new column called
{name}
on the far right, a list column containing data frames the same type asy
.
Relationship to other joins
You can recreate many other joins from the result of a nest join:
-
inner_join()
is anest_join()
plustidyr::unnest()
. -
left_join()
is anest_join()
plustidyr::unnest(keep_empty = TRUE)
. -
semi_join()
is anest_join()
plus afilter()
where you check that every element of data has at least one row. -
anti_join()
is anest_join()
plus afilter()
where you check that every element has zero rows.
Methods
This function is a generic, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.
The following methods are currently available in loaded packages: no methods found.
See Also
Other joins:
cross_join()
,
filter-joins
,
mutate-joins
Examples
df1 <- tibble(x = 1:3)
df2 <- tibble(x = c(2, 3, 3), y = c("a", "b", "c"))
out <- nest_join(df1, df2)
out
out$df2