named: Subset objects by name

namedR Documentation

Subset objects by name

Description

x |> named(y) is the same as x[y], but

Usage

named(x, y)

Arguments

x

An object with names.

y

A vector of names.

Details

  • 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.

Value

setNames(x[y], y).

Examples


vec <- c(one = 1, two = 2, three = 3, four = 4)
nms <- c("three", "two", "five")
vec |> named(nms)
# Compare to:
vec[nms]

hughjonesd/rumpel documentation built on Jan. 22, 2025, 1:12 a.m.