TidierOrg / TidierData.jl

Tidier data transformations in Julia, modeled after the dplyr/tidyr R packages.
MIT License
85 stars 7 forks source link

Programmatic Renames #120

Open mrufsvold opened 2 weeks ago

mrufsvold commented 2 weeks ago

I'd like to rename columns with a Dictionary or a Function. @rename only allows for static assignment (easily... I still don't fully have my head around interpolation).

Would it be possible to add @rename_with(x) which passes x to DataFrames.rename, where x is interpolated automatically? One small quirk of doing this is that the position of the DataFrame is different for different methods of DataFrames.rename:

rename(df, dict)
# or
rename(f, df)

The later supports do syntax, but it would mean some type detection would need to happen to figure out which method to call with @rename_with.

Edit: As I wrote this, it might make sense to add @select_with(x) which does a similar thing (returns df[ : , x()]).

drizk1 commented 2 weeks ago

You can rename columns with a Dict. (Sorry this is as a photo because I'm afk right now) would something like this work for what you need? Using !!Dict you can pass a Dict to rename to rename multiple columns at once

@rename_with supports renaming with a function as well

image


julia> @rename_with(df, str -> str_remove_all(str, "_a"), !term_a)
mrufsvold commented 2 weeks ago

Oh, great! Did I miss this in the docs or is this actually a request for this to be documented?

drizk1 commented 2 weeks ago

No this was something I tried doing after seeing it done on Twitter in tidyverse and I had a screenshot handy. Putting an example in the docstrings is a great idea tho