Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

set_names

Set names of a vector


Description

Stable lifecycle

This is equivalent to stats::setNames(), with more features and stricter argument checking.

Usage

set_names(x, nm = x, ...)

Arguments

x

Vector to name.

nm, ...

Vector of names, the same length as x.

You can specify names in the following ways:

  • If you do nothing, x will be named with itself.

  • If x already has names, you can provide a function or formula to transform the existing names. In that case, ... is passed to the function.

  • If nm is NULL, the names are removed (if present).

  • In all other cases, nm and ... are coerced to character.

Life cycle

set_names() is stable and exported in purrr.

Examples

set_names(1:4, c("a", "b", "c", "d"))
set_names(1:4, letters[1:4])
set_names(1:4, "a", "b", "c", "d")

# If the second argument is ommitted a vector is named with itself
set_names(letters[1:5])

# Alternatively you can supply a function
set_names(1:10, ~ letters[seq_along(.)])
set_names(head(mtcars), toupper)

# If the input vector is unnamed, it is first named after itself
# before the function is applied:
set_names(letters, toupper)

# `...` is passed to the function:
set_names(head(mtcars), paste0, "_foo")

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.