tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.79k stars 2.12k forks source link

Clarity of "duplicated" error message in `rename_with()` #7006

Open DanChaltiel opened 8 months ago

DanChaltiel commented 8 months ago

Hi,

When using rename_with() and ending with duplicated column names, the error message tells us about the transformed, duplicated name and the location of the original columns.

However, it would be much clearer to have the original column names in the error message.

The locations are not of much help if rename_with() is used in a long pipeline, nor is the transformed name if the function is not well described.

Here is an example case:

library(tidyverse)
letter_sum = function(x){
  x %>% map_chr(~{str_split_1(.x, "") %>% map_int(~which(letters==tolower(.x))) %>% sum() %>% as.character()})
}
mtcars %>% rename_with(letter_sum)
#> Error in `rename_with()`:
#> ! Names must be unique.
#> x These names are duplicated:
#>   * "24" at locations 4 and 11.
#>   * "43" at locations 5 and 6.

Created on 2024-03-28 with reprex v2.1.0

While I admit the transformation is exaggeratedly nonsensical, it shows that the error is not very useful in such cases.