nest-mutate-joins {nplyr} | R Documentation |
Nested Mutating joins
Description
Nested mutating joins add columns from y
to each of the nested data frames
in .nest_data
, matching observations based on the keys. There are four
nested mutating joins:
Inner join
nest_inner_join()
only keeps observations from .nest_data
that have a
matching key in y
.
The most important property of an inner join is that unmatched rows in either input are not included in the result.
Outer joins
There are three outer joins that keep observations that appear in at least one of the data frames:
-
nest_left_join()
keeps all observations in.nest_data
. -
nest_right_join()
keeps all observations iny
. -
nest_full_join()
keeps all observations in.nest_data
andy
.
Usage
nest_inner_join(
.data,
.nest_data,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE
)
nest_left_join(
.data,
.nest_data,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE
)
nest_right_join(
.data,
.nest_data,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE
)
nest_full_join(
.data,
.nest_data,
y,
by = NULL,
copy = FALSE,
suffix = c(".x", ".y"),
...,
keep = FALSE
)
Arguments
.data |
A data frame, data frame extension (e.g., a tibble), or a lazy data frame (e.g., from dbplyr or dtplyr). |
.nest_data |
A list-column containing data frames |
y |
A data frame, data frame extension (e.g., a tibble), or a lazy data frame (e.g., from dbplyr or dtplyr). |
by |
A character vector of variables to join by or a join specification
created with If To join on different variables between the objects in To join by multiple variables, use a vector with length >1. For example,
To perform a cross-join, generating all combinations of each object in
|
copy |
If |
suffix |
If there are non-joined duplicate variables in |
... |
Other parameters passed onto methods. Includes:
|
keep |
Should the join keys from both |
Details
nest_inner_join()
, nest_left_join()
, nest_right_join()
, and
nest_full_join()
are largely wrappers for dplyr::inner_join()
,
dplyr::left_join()
, dplyr::right_join()
, and dplyr::full_join()
and
maintain the functionality of these verbs within each nested data frame. For
more information on inner_join()
, left_join()
, right_join()
, or
full_join()
, please refer to the documentation in
dplyr
.
Value
An object of the same type as .data
. Each object in the column .nest_data
will also be of the same type as the input. The order of the rows and columns
of each object in .nest_data
is preserved as much as possible. Each object
in .nest_data
has the following properties:
For
nest_inner_join()
, a subset of rows in each object in.nest_data
. Fornest_left_join()
, all rows in each object in.nest_data
. Fornest_right_join()
, a subset of rows in each object in.nest_data
, followed by unmatchedy
rows. Fornest_full_join()
, all rows in each object in.nest_data
, followed by unmatchedy
rows.Output columns include all columns from each
.nest_data
and all non-key columns fromy
. Ifkeep = TRUE
, the key columns fromy
are included as well.If non-key columns in any object in
.nest_data
andy
have the same name,suffix
es are added to disambiguate. Ifkeep = TRUE
and key columns in.nest_data
andy
have the same name,suffix
es are added to disambiguate these as well.If
keep = FALSE
, output columns included inby
are coerced to their common type between the objects in.nest_data
andy
.Groups are taken from
.nest_data
.
See Also
Other joins:
nest-filter-joins
,
nest_nest_join()
Examples
gm_nest <- gapminder::gapminder %>% tidyr::nest(country_data = -continent)
gm_codes <- gapminder::country_codes
gm_nest %>% nest_inner_join(country_data, gm_codes, by = "country")
gm_nest %>% nest_left_join(country_data, gm_codes, by = "country")
gm_nest %>% nest_right_join(country_data, gm_codes, by = "country")
gm_nest %>% nest_full_join(country_data, gm_codes, by = "country")