Let's say I have a data frame with around 150 variables that I need to change their names, based on a separate CSV file (an "index"). The order of the vars is different between the data frame and the index, and to make things worse, their are some vars that maybe missing in the index and vice versa.
Is there an elegant way or a package that can help me do so?
Here is an example of the data I'm dealing with.
library(dplyr)
library(tibble)
##My original data frame
orig <- tribble(
~item, ~protein, ~carbohydrates, ~total_fat, ~energy, ~zinc,
1, 5.4, 10.6, 7.3, 90, 3.4,
2, 10.3, 11.6, 3.3, 10, 2.1,
3, 8.4, 10.6, 2.3, 52, 0.2,
4, 2.7, 8.6, 20.3, 356, 1.3)
##New names index
csv_nm <- tribble(
~new_name, ~old_name,
"nut203", "protein",
"nut204", "total_fat",
"nut205", "carbohydrates",
"nut208", "energy",
"nut303", "iron")
I tried using vectors as kindly suggested by Peter here:
## create a named vector to use with dplyr::rename
nm_vec <- csv_nm$old_name
names(nm_vec) <- csv_nm$new_name
## rename, subsetting the named vector to exclude names which are not present in the dataframe
tib_new_names <-
orig %>%
rename(nm_vec[nm_vec %in% names(orig)])
But got "All arguments must be named" error.