Programmatically rename data frame columns using lookup data frame

Viewed 485

What is the best way to batch rename columns using a lookup data frame?

Can I do it as part of a pipe?

library(tidyverse)

df <- data_frame(
  a = seq(1, 10)
  , b = seq(10, 1)
  , c = rep(1, 10)
)

df_lookup <- data_frame(
  old_name = c("b", "c", "a")
  , new_name = c("y", "z", "x")
)

I know how to do it manually

df %>% 
  rename(x = a
    , y = b
    , z = c)

I am seeking a solution in tidyverse / dplyr packages.

3 Answers
Related