join | R Documentation |
matrixset
or a data.frame
The operation is done through a join operation between the row meta info
data.frame (join_row_info()
) of .ms
and y
(or its row meta info
data.frame if it is a matrixset
object). The function join_column_info()
does the equivalent operation for column meta info.
The default join operation is a left join (type == 'left'), but most of dplyr's joins are available ('left', 'inner', 'right', 'full', 'semi' or 'anti').
The matrixset
paradigm of unique row/column names is enforced so if a
.ms
data.frame row matches multiple ones in y
, the default behavior is
to issue a condition error.
This can be modified by setting new tag names via the argument names_glue
.
join_row_info(
.ms,
y,
type = "left",
by = NULL,
adjust = FALSE,
names_glue = NULL,
suffix = c(".x", ".y"),
na_matches = c("na", "never")
)
join_column_info(
.ms,
y,
type = "left",
by = NULL,
adjust = FALSE,
names_glue = NULL,
suffix = c(".x", ".y"),
na_matches = c("na", "never")
)
.ms |
A |
y |
A |
type |
Joining type, one of 'left', 'inner', 'right', 'full', 'semi' or 'anti'. |
by |
The names of the variable to join by.
The default, |
adjust |
A logical. By default ( Alternatively,
Other values are padded with |
names_glue |
a parameter that may allow multiple matches. By default,
( The value of Finally, When making the unique tag names, only the non-unique names are modified.
Also, |
suffix |
Suffixes added to disambiguate trait variables. See
|
na_matches |
How to handle missing values when matching. See
|
A matrixset
with updated row or column meta info, with all .ms
traits and
y
traits. If some traits share the same names - and were not included in
by
- suffix
es will be appended to these names.
If adjustment was allowed, the dimensions of the new matrixset
may differ
from the original one.
When y
is a matrixset
, only groups from .ms
are used, if any. Group
update is the same as in dplyr
.
ms1 <- remove_row_annotation(student_results, class, teacher)
ms <- join_row_info(ms1, student_results)
ms <- join_row_info(ms1, student_results, by = c(".rowname", "previous_year_score"))
# This will throw an error
ms2 <- remove_row_annotation(filter_row(student_results, class %in% c("classA", "classC")),
class, teacher, previous_year_score)
ms <- tryCatch(join_row_info(ms2, student_results, type = "full"),
error = function(e) e)
is(ms, "error") # TRUE
ms$message
# Now it works.
ms <- join_row_info(ms2, student_results, type = "full", adjust = TRUE)
dim(ms2)
dim(ms)
matrix_elm(ms, 1)
# Similarly, this will fail because tag names are no longer unique
meta <- tibble::tibble(sample = c("student 2", "student 2"),
msr = c("height", "weight"),
value = c(145, 32))
ms <- tryCatch(join_row_info(student_results, meta, by = c(".rowname"="sample")),
error = function(e) e)
is(ms, "error") # TRUE
ms$message
# This works, by forcing the tag names to be unique. Notice that we suppress
# the warning for now. We'll come back to it.
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = TRUE)
)
# Here's the warning: we're being told there was a change in tag names
(purrr::quietly(join_row_info)(student_results, meta,
by = c(".rowname"="sample"), adjust = TRUE,
names_glue = TRUE))$warnings
# You can have better control on how the tag change occurs, for instance by
# appending the msr value to the name
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = "{.tag}_{msr}")
)
# In this specific example, the {.tag} was superfluous, since the default is
# to append after the tag name
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = "{msr}")
)
# But the keyword is useful if you want to shuffle order
suppressWarnings(
join_row_info(student_results, meta, by = c(".rowname"="sample"),
adjust = TRUE, names_glue = "{msr}.{.tag}")
)
# You are warned when there is a change in traits
meta <- tibble::tibble(sample = c("student 2", "student 2"),
class = c("classA", "classA"),
msr = c("height", "weight"),
value = c(145, 32))
(purrr::quietly(join_row_info)(student_results, meta,
by = c(".rowname"="sample"), adjust = TRUE,
names_glue = TRUE))$warnings[2]
# Groups are automatically adjusted
sr_gr <- row_group_by(student_results, class)
gr_orig <- row_group_meta(row_group_by(student_results, class)) |> tidyr::unnest(.rows)
suppressWarnings(
new_gr <- join_row_info(sr_gr, meta, by = c(".rowname" = "sample", "class"),
adjust = TRUE, names_glue = TRUE) |>
row_group_meta() |> tidyr::unnest(.rows)
)
list(gr_orig, new_gr)
# In the example above, the join operation changed the class of 'class',
# which in turn changed the grouping meta info. You are warned of both.
(purrr::quietly(join_row_info)(sr_gr, meta,
by = c(".rowname"="sample", "class"),
adjust = TRUE, names_glue = TRUE))$warnings
# A change in trait name that was used for grouping will result in losing the
# grouping. You are warning of the change in grouping structure.
(purrr::quietly(join_row_info)(sr_gr, meta,
by = c(".rowname"="sample"),
adjust = TRUE, names_glue = TRUE))$warnings
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.