rename_dict: Dictionary Style Renaming

View source: R/rename_dict.R

rename_dictR Documentation

Dictionary Style Renaming

Description

Dictionary Style Renaming

Usage

rename_dict(df, old_names, new_names, match_all = TRUE)

Arguments

df

A data frame

old_names

Character vector of names in df

new_names

Character vector of new names of the same length as old_names

match_all

Do all of old_names need to be in the columns of df?

Value

df with new names

Note

I like option in pandas to rename using a dictionary so here is an R version.

Examples

df = tibble::tibble(
  Hammurabi = 1:3,
  Cyrus = letters[1:3],
  David = LETTERS[1:3]
)

rename_from = c(
  'Cyrus',
  'Hammurabi',
  'Alexander'
)

rename_to = c(
  'Cambyses',
  'Nebuchadnezzar',
  'Ptolemy'
)

rename_dict(
  df,
  old_names = rename_from,
  new_names = rename_to,
  match_all = FALSE
)

svenhalvorson/SvenR documentation built on Aug. 25, 2023, 1:31 p.m.