named | R Documentation |
x |> named(y)
is the same as x[y]
, but
named(x, y)
x |
An object with names. |
y |
A vector of names. |
it makes your intent clearer;
it throws an error if y
is not a character vector;
unmatched elements of y
return NA
, but with the name of
the element, whereas x[y]
would return an NA
name.
setNames(x[y], y)
.
vec <- c(one = 1, two = 2, three = 3, four = 4)
nms <- c("three", "two", "five")
vec |> named(nms)
# Compare to:
vec[nms]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.