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

Include dplyr function rename in tidylog package #27

Closed
damianooldoni opened this issue Jul 1, 2019 · 4 comments
Closed

Include dplyr function rename in tidylog package #27

damianooldoni opened this issue Jul 1, 2019 · 4 comments

Comments

@damianooldoni
Copy link
Contributor

damianooldoni commented Jul 1, 2019

While using this very useful package, I find it would be nice to get information also about column renaming while working with dplyr::rename() in pipes.

@elbersb
Copy link
Owner

elbersb commented Sep 17, 2019

This is a bit tricky because it’s impossible to tell how the columns were renamed from just inspecting the data frame before and after. This means one has to gather this information from the arguments, which is error-prone. Any ideas would be appreciated.

@damianooldoni
Copy link
Contributor Author

This is a bit tricky because it’s impossible to tell how the columns were renamed from just inspecting the data frame before and after.

Maybe by comparing column names by commando colnames(df) before and after?

> a <- data.frame(col1 = c(1,2), col2 = c(3,4))
> name_cols_before <- colnames(a)
> name_cols_before
[1] "col1" "col2"
> a <- dplyr::rename(a, newcol1 = col1)
> name_cols_after <- colnames(a)
> name_cols_after 
[1] "newcol1" "col2"
> paste(
  "Column", 
  name_cols_before[name_cols_after != name_cols_before], 
  "renamed as", 
  name_cols_after[name_cols_after != name_cols_before]
)
[1] "Column col1 renamed as newcol1"

This is for dplyr::rename, likely not for multiple renames via rename_at or rename_if.

Or did I misunderstand your reply? Still thanks for paying attention to this issue.

@elbersb
Copy link
Owner

elbersb commented Sep 17, 2019

Right, that's the approach that's being used for all the other functions. But I always assumed that rename would reorder the columns, which would make this strategy impossible. It seems though that rename does not reorder columns. This would mean all the column positions match up, and it shouldn't be a problem to implement rename, rename_at, rename_all, etc. Could you check whether this understanding is correct? The docs for dplyr don't mention this explicitly, so maybe we can experiment a bit more.

a <- data.frame(col1 = c(1,2), col2 = c(3,4))
names(a)
#> [1] "col1" "col2"
names(dplyr::rename(a, newcol1 = col1, newcol2 = col2))
#> [1] "newcol1" "newcol2"
names(dplyr::rename(a, newcol1 = col2, newcol2 = col1))
#> [1] "newcol2" "newcol1"
names(dplyr::rename(a, newcol2 = col2, newcol1 = col1))
#> [1] "newcol1" "newcol2"

Created on 2019-09-17 by the reprex package (v0.3.0)

@damianooldoni
Copy link
Contributor Author

Thanks @elbersb : yes, I played a little with rename_* functions as well and no changes in order of columns detected.

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

2 participants