I try to rename columns of a dataframe, via a correspondence table, a two-columns dataframe (name before and name after).
In my situation, the to-rename dataframe may have columns not referenced in my correspondence table (it's not a problem, they won't be renamed), and, at the opposite, the correspondence table may contain names not included in my dataframe. This list may be used for other dataframes.
And, at least, the order of the names may not be the same in the 2 dataframes, it would be too easy.
I have a solution with a loop, but try to find another solution, with no loop.
Any idea ? Thanks in advance.
Here is my code
df_to_rename <- data.frame(annee = 2010:2012,
code_commune = 67000:67002,
duree_occup_ou_vacance_tranche = 1:3,
nb_total_logements = 100:102,
secret_not_in_list = 1:3)
liste_noms_col <- data.frame(noms_origine = c("mode_occ",
"code_commune",
"annee",
"duree_occup_ou_vacance_tranche",
"nb_total_logements"),
noms_nouveaux = c("mode d\'occupation - not included",
"code INSEE commune",
"Année",
"durée d\'occupation (ou vacance)",
"Nombre de logements"))
##### Rename by loop
l_noms <- df_to_rename %>% names()
# (i_nom <- l_noms[1])
for(i_nom in l_noms) {
nom_a_changer <-
liste_noms_col[liste_noms_col$noms_origine == i_nom, "noms_nouveaux"]
# message(i_nom," -> ",nom_a_changer)
if(length(nom_a_changer)>0) {
df_to_rename <- df_to_rename %>%
rename({{nom_a_changer}} := {{i_nom}})
message(i_nom," -> ",nom_a_changer)
}
}
df_to_rename %>% names()