Mutating joins add columns from y
to x
, matching observations based on
the keys. There are four mutating joins: the inner join, and the three outer
joins.
An inner_join()
only keeps observations from x
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. This means that generally inner joins are not appropriate in most analyses, because it is too easy to lose observations.
The three outer joins keep observations that appear in at least one of the data frames:
A left_join()
keeps all observations in x
.
A right_join()
keeps all observations in y
.
A full_join()
keeps all observations in x
and y
.
# S3 method for class 'SpatialExperiment'
right_join(x, y, by = NULL, copy = FALSE, suffix = c(".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.
A join specification created with join_by()
, or a character
vector of variables to join by.
If NULL
, the default, *_join()
will perform a natural join, using all
variables in common across x
and y
. A message lists the variables so
that you can check they're correct; suppress the message by supplying by
explicitly.
To join on different variables between x
and y
, use a join_by()
specification. For example, join_by(a == b)
will match x$a
to y$b
.
To join by multiple variables, use a join_by()
specification with
multiple expressions. For example, join_by(a == b, c == d)
will match
x$a
to y$b
and x$c
to y$d
. If the column names are the same between
x
and y
, you can shorten this by listing only the variable names, like
join_by(a, c)
.
join_by()
can also be used to perform inequality, rolling, and overlap
joins. See the documentation at ?join_by for details on
these types of joins.
For simple equality joins, you can alternatively specify a character vector
of variable names to join by. For example, by = c("a", "b")
joins x$a
to y$a
and x$b
to y$b
. If variable names differ between x
and y
,
use a named character vector like by = c("x_a" = "y_a", "x_b" = "y_b")
.
To perform a cross-join, generating all combinations of x
and y
, see
cross_join()
.
If x
and y
are not from the same data source,
and copy
is TRUE
, then y
will be copied into the
same src as x
. This allows you to join tables across srcs, but
it is a potentially expensive operation so you must opt into it.
If there are non-joined duplicate variables in x
and
y
, these suffixes will be added to the output to disambiguate them.
Should be a character vector of length 2.
Other parameters passed onto methods.
An object of the same type as x
(including the same groups). The order of
the rows and columns of x
is preserved as much as possible. The output has
the following properties:
The rows are affect by the join type.
inner_join()
returns matched x
rows.
left_join()
returns all x
rows.
right_join()
returns matched of x
rows, followed by unmatched y
rows.
full_join()
returns all x
rows, followed by unmatched y
rows.
Output columns include all columns from x
and all non-key columns from
y
. If keep = TRUE
, the key columns from y
are included as well.
If non-key columns in x
and y
have the same name, suffix
es are added
to disambiguate. If keep = TRUE
and key columns in x
and y
have
the same name, suffix
es are added to disambiguate these as well.
If keep = FALSE
, output columns included in by
are coerced to their
common type between x
and y
.
By default, dplyr guards against many-to-many relationships in equality joins by throwing a warning. These occur when both of the following are true:
A row in x
matches multiple rows in y
.
A row in y
matches multiple rows in x
.
This is typically surprising, as most joins involve a relationship of one-to-one, one-to-many, or many-to-one, and is often the result of an improperly specified join. Many-to-many relationships are particularly problematic because they can result in a Cartesian explosion of the number of rows returned from the join.
If a many-to-many relationship is expected, silence this warning by
explicitly setting relationship = "many-to-many"
.
In production code, it is best to preemptively set relationship
to whatever
relationship you expect to exist between the keys of x
and y
, as this
forces an error to occur immediately if the data doesn't align with your
expectations.
Inequality joins typically result in many-to-many relationships by nature, so they don't warn on them by default, but you should still take extra care when specifying an inequality join, because they also have the capability to return a large number of rows.
Rolling joins don't warn on many-to-many relationships either, but many
rolling joins follow a many-to-one relationship, so it is often useful to
set relationship = "many-to-one"
to enforce this.
Note that in SQL, most database providers won't let you specify a many-to-many relationship between two tables, instead requiring that you create a third junction table that results in two one-to-many relationships instead.
These functions are generics, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.
Methods available in currently loaded packages:
inner_join()
: (SingleCellExperiment
), dplyr (data.frame
), tidySpatialExperiment (SpatialExperiment
)
.
left_join()
: (SingleCellExperiment
), dplyr (data.frame
), tidySpatialExperiment (SpatialExperiment
)
.
right_join()
: (SingleCellExperiment
), dplyr (data.frame
), tidySpatialExperiment (SpatialExperiment
)
.
full_join()
: (SingleCellExperiment
), dplyr (data.frame
)
.
Other joins:
cross_join()
,
filter-joins
,
nest_join()
example(read10xVisium)
#>
#> rd10xV> dir <- system.file(
#> rd10xV+ file.path("extdata", "10xVisium"),
#> rd10xV+ package = "SpatialExperiment")
#>
#> rd10xV> sample_ids <- c("section1", "section2")
#>
#> rd10xV> samples <- file.path(dir, sample_ids, "outs")
#>
#> rd10xV> list.files(samples[1])
#> [1] "raw_feature_bc_matrix" "spatial"
#>
#> rd10xV> list.files(file.path(samples[1], "spatial"))
#> [1] "scalefactors_json.json" "tissue_lowres_image.png"
#> [3] "tissue_positions_list.csv"
#>
#> rd10xV> file.path(samples[1], "raw_feature_bc_matrix")
#> [1] "/__w/_temp/Library/SpatialExperiment/extdata/10xVisium/section1/outs/raw_feature_bc_matrix"
#>
#> rd10xV> (spe <- read10xVisium(samples, sample_ids,
#> rd10xV+ type = "sparse", data = "raw",
#> rd10xV+ images = "lowres", load = FALSE))
#> # A SpatialExperiment-tibble abstraction: 99 × 7
#> # Features = 50 | Cells = 99 | Assays = counts
#> .cell in_tissue array_row array_col sample_id pxl_col_in_fullres
#> <chr> <lgl> <int> <int> <chr> <int>
#> 1 AAACAACGAATAGTTC-1 FALSE 0 16 section1 2312
#> 2 AAACAAGTATCTCCCA-1 TRUE 50 102 section1 8230
#> 3 AAACAATCTACTAGCA-1 TRUE 3 43 section1 4170
#> 4 AAACACCAATAACTGC-1 TRUE 59 19 section1 2519
#> 5 AAACAGAGCGACTCCT-1 TRUE 14 94 section1 7679
#> 6 AAACAGCTTTCAGAAG-1 FALSE 43 9 section1 1831
#> 7 AAACAGGGTCTATATT-1 FALSE 47 13 section1 2106
#> 8 AAACAGTGTTCCTGGG-1 FALSE 73 43 section1 4170
#> 9 AAACATGGTGAGAGGA-1 FALSE 62 0 section1 1212
#> 10 AAACATTTCCCGGATT-1 FALSE 61 97 section1 7886
#> # ℹ 89 more rows
#> # ℹ 1 more variable: pxl_row_in_fullres <int>
#>
#> rd10xV> # base directory 'outs/' from Space Ranger can also be omitted
#> rd10xV> samples2 <- file.path(dir, sample_ids)
#>
#> rd10xV> (spe2 <- read10xVisium(samples2, sample_ids,
#> rd10xV+ type = "sparse", data = "raw",
#> rd10xV+ images = "lowres", load = FALSE))
#> # A SpatialExperiment-tibble abstraction: 99 × 7
#> # Features = 50 | Cells = 99 | Assays = counts
#> .cell in_tissue array_row array_col sample_id pxl_col_in_fullres
#> <chr> <lgl> <int> <int> <chr> <int>
#> 1 AAACAACGAATAGTTC-1 FALSE 0 16 section1 2312
#> 2 AAACAAGTATCTCCCA-1 TRUE 50 102 section1 8230
#> 3 AAACAATCTACTAGCA-1 TRUE 3 43 section1 4170
#> 4 AAACACCAATAACTGC-1 TRUE 59 19 section1 2519
#> 5 AAACAGAGCGACTCCT-1 TRUE 14 94 section1 7679
#> 6 AAACAGCTTTCAGAAG-1 FALSE 43 9 section1 1831
#> 7 AAACAGGGTCTATATT-1 FALSE 47 13 section1 2106
#> 8 AAACAGTGTTCCTGGG-1 FALSE 73 43 section1 4170
#> 9 AAACATGGTGAGAGGA-1 FALSE 62 0 section1 1212
#> 10 AAACATTTCCCGGATT-1 FALSE 61 97 section1 7886
#> # ℹ 89 more rows
#> # ℹ 1 more variable: pxl_row_in_fullres <int>
#>
#> rd10xV> # tabulate number of spots mapped to tissue
#> rd10xV> cd <- colData(spe)
#>
#> rd10xV> table(
#> rd10xV+ in_tissue = cd$in_tissue,
#> rd10xV+ sample_id = cd$sample_id)
#> sample_id
#> in_tissue section1 section2
#> FALSE 28 27
#> TRUE 22 22
#>
#> rd10xV> # view available images
#> rd10xV> imgData(spe)
#> DataFrame with 2 rows and 4 columns
#> sample_id image_id data scaleFactor
#> <character> <character> <list> <numeric>
#> 1 section1 lowres #### 0.0510334
#> 2 section2 lowres #### 0.0510334
spe |>
right_join(
spe |>
filter(in_tissue == TRUE) |>
mutate(new_column = 1)
)
#> Joining with `by = join_by(.cell, in_tissue, array_row, array_col, sample_id)`
#> # A SpatialExperiment-tibble abstraction: 44 × 8
#> # Features = 50 | Cells = 44 | Assays = counts
#> .cell in_tissue array_row array_col sample_id new_column pxl_col_in_fullres
#> <chr> <lgl> <int> <int> <chr> <dbl> <int>
#> 1 AAACAA… TRUE 50 102 section1 1 8230
#> 2 AAACAA… TRUE 3 43 section1 1 4170
#> 3 AAACAC… TRUE 59 19 section1 1 2519
#> 4 AAACAG… TRUE 14 94 section1 1 7679
#> 5 AAACCG… TRUE 42 28 section1 1 3138
#> 6 AAACCG… TRUE 52 42 section1 1 4101
#> 7 AAACCT… TRUE 37 19 section1 1 2519
#> 8 AAACGA… TRUE 6 64 section1 1 5615
#> 9 AAACGA… TRUE 35 79 section1 1 6647
#> 10 AAACGG… TRUE 67 59 section1 1 5271
#> # ℹ 34 more rows
#> # ℹ 1 more variable: pxl_row_in_fullres <int>