Skip to content

Instantly share code, notes, and snippets.

@PietrH
Created November 13, 2023 12:24
Show Gist options
  • Save PietrH/8ca7c8ba61d954d164d7164c45d47b71 to your computer and use it in GitHub Desktop.
Save PietrH/8ca7c8ba61d954d164d7164c45d47b71 to your computer and use it in GitHub Desktop.
Rename columns in a data.frame based on a second data.frame as a lookup table
first_df <- tibble::tibble(ids = c("id1", "id2", "id3"), labels = c("label1", "label2", "label3"))
second_df <- tibble::tibble(id1 = 1:3, id2 = 3:5, id3 = letters[6:8])
dplyr::rename_with(second_df, ~ first_df$labels[match(.x, first_df$ids)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment