tidyverse / dplyr

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

`rename` keeps the old column name #7095

Closed adupaix closed 1 month ago

adupaix commented 1 month ago

The old column of a data frame is somehow "kept" after using the rename function. It happens only when the new name starts with the old name.

df <- data.frame(a = 1:10)
df %>% dplyr::rename('a_a' = 'a') -> df
df$a_a
#  [1]  1  2  3  4  5  6  7  8  9 10
'a' %in% names(df)
# [1] FALSE
df$a
# [1]  1  2  3  4  5  6  7  8  9 10
sda030 commented 1 month ago

This is not a bug, when you are using df$a, R uses partial matching to grab a_a.

tmp <- data.frame(a_a = 1:10)
tmp$a
#>  [1]  1  2  3  4  5  6  7  8  9 10

Created on 2024-10-22 with reprex v2.1.1

DavisVaughan commented 1 month ago

@sda030 is right