'Rename Error in dplyr: Names must be Unique

Despite having unique names, dplyr::rename breaks and gives "Error: Names must be unique." I know I could opt to rename in other methods such as the following:

rename <- function(dat, oldnames, newnames) 
{  datnames <- colnames(dat)
   datnames[which(datnames %in% oldnames)] <- newnames
   colnames(dat) <- datnames
   dat
 }

Here is my code:

df1 <- dplyr::rename(df,
  "Language1"="Language",
  "RESPAge"="Age...31",
  "ADMIN1Name"="ADM1_NAME",
  "RESPSex"="Gender...42",
  "HHSize"="RspIsHoH",
  "HWaterSRC"="WaterSource",
  "NoteFCS"="Note FCS",
  "FCSStap"="Staples",
  "FCSPulse"="Pulses",
  "FCSDairy"="Dairy",
  "FCSPr"="Proteins",
  "FCSVeg"="Veg",
  "FCSFruit"="Fruits",
  "FCSFat"="Fats",
  "FCSSugar"="Sugars",
  "noterCSI"="note_Rcsi",
  "rCSILessQlty"="LessExpensiveFood",
  "rCSIBorrow"="BorrowOrHelp",
  "rCSIMealSize"="LimitPortionSize",
  "rCSIMealNb"="ReduceNumMeals",
  "rCSIMealAdult"="RestrictConsumption")

But I want to use dplyr for this. I thought it was a conflict because of other loaded packages and tried only loading readxl(for reading data) and dplyr but it didn't change the result.



Solution 1:[1]

The data frame had a column (index) with no header that caused the code to break. Dropping the column solved the challenge.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Denis Mwaniki