Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename keeps the old column name #7095

Closed
adupaix opened this issue Oct 22, 2024 · 2 comments
Closed

rename keeps the old column name #7095

adupaix opened this issue Oct 22, 2024 · 2 comments

Comments

@adupaix
Copy link

adupaix commented Oct 22, 2024

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
Copy link

sda030 commented Oct 22, 2024

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
Copy link
Member

@sda030 is right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants