rename_lookup: Rename by looking up names in a table

rename_lookupR Documentation

Rename by looking up names in a table

Description

This is useful when you have a vector of old names and a vector of new names, or columns in a data frame.

Usage

rename_lookup(x, old, new, warn = FALSE)

Arguments

x

An object with names.

old

Character vector. Existing names will be found using match(names(x), old)

new

Character vector. A vector of new names to replace corresponding elements in old.

warn

Logical. Warn if any names are unmatched?

Details

Unmatched names are left unchanged.

Value

x renamed according to names(x) <- new[match(names(x), old)].

Examples


df <- data.frame(
        old = c("One", "Two", "Three"),
        new = c("New", "Newer", "Newest")
      )
vec <- c("One" = 1, "Two" = 2, "Three" = 3, "Four" = 4)
vec |> rename_lookup(df$old, df$new)


hughjonesd/namer documentation built on Jan. 23, 2025, 5:28 p.m.