| list_combine | R Documentation |
list_combine() is a more powerful version of vec_c(). While vec_c() is
used for sequential combination, list_combine() takes a list of indices
that specify where to place each element in the output.
If you have a list of vectors and just need to combine them sequentially,
you'll still want to use vec_c(!!!x).
list_combine(
x,
...,
indices,
size,
default = NULL,
unmatched = "default",
multiple = "last",
slice_x = FALSE,
ptype = NULL,
name_spec = NULL,
name_repair = c("minimal", "unique", "check_unique", "universal", "unique_quiet",
"universal_quiet"),
x_arg = "x",
indices_arg = "indices",
default_arg = "default",
error_call = current_env()
)
x |
A list of vectors. If If |
... |
These dots are for future extensions and must be empty. |
indices |
A list of indices. Indices can be provided in one of two forms:
The size of |
size |
The output size. |
default |
If If provided, Can only be set when |
unmatched |
Handling of locations in the output unmatched by
|
multiple |
Handling of locations in the output matched by multiple
Note that |
slice_x |
A boolean. If See the |
ptype |
If |
name_spec |
A name specification for combining
inner and outer names. This is relevant for inputs passed with a
name, when these inputs are themselves named, like
See the name specification topic. |
name_repair |
How to repair names, see |
x_arg, indices_arg, default_arg |
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem. |
error_call |
The execution environment of a currently
running function, e.g. |
A vector of type vec_ptype_common(!!!x), or ptype, if specified.
The size of the output is determined by size.
# Combine a list of vectors using
# a list of `indices`
x <- list(
1:3,
4:6,
7:8
)
indices <- list(
c(1, 3, 7),
c(8, 6, 5),
c(2, 4)
)
list_combine(x, indices = indices, size = 8)
# Overlapping `indices` are allowed.
# The last match "wins" by default.
x <- list(
1:3,
4:6
)
indices <- list(
c(1, 2, 3),
c(1, 2, 6)
)
list_combine(x, indices = indices, size = 6)
# Use `multiple` to force the first match to win.
# This is similar to how `dplyr::case_when()` works.
list_combine(x, indices = indices, size = 6, multiple = "first")
# Works with data frames as well.
# Now how index 2 is not assigned to.
x <- list(
data.frame(x = 1:2, y = c("a", "b")),
data.frame(x = 3:4, y = c("c", "d"))
)
indices <- list(
c(4, 1),
c(3, NA)
)
list_combine(x, indices = indices, size = 4)
# You can use `size` to combine into a larger object than you have values for
list_combine(list(1:2, 4:5), indices = list(1:2, 4:5), size = 8)
# Additionally specifying `default` allows you to control the value used in
# unfilled locations
list_combine(
list(1:2, 4:5),
indices = list(1:2, 4:5),
size = 8,
default = 0L
)
# Alternatively, if you'd like to assert that you've covered all output
# locations through `indices`, set `unmatched = "error"`.
# Here, we've set the size to 5 but missed location 3:
try(list_combine(
list(1:2, 4:5),
indices = list(1:2, 4:5),
size = 5,
unmatched = "error"
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.