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

Allow glue-styled pattern for data_rename() #563

Merged
merged 13 commits into from
Nov 27, 2024
Merged

Conversation

strengejacke
Copy link
Member

library(datawizard)
head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}"))
#>                   formerly_mpg formerly_cyl formerly_disp
#> Mazda RX4                 21.0            6           160
#> Mazda RX4 Wag             21.0            6           160
#> Datsun 710                22.8            4           108
#> Hornet 4 Drive            21.4            6           258
#> Hornet Sportabout         18.7            8           360
#> Valiant                   18.1            6           225
head(data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}"))
#>                   mpg_is_column_1 cyl_is_column_2 disp_is_column_3
#> Mazda RX4                    21.0               6              160
#> Mazda RX4 Wag                21.0               6              160
#> Datsun 710                   22.8               4              108
#> Hornet 4 Drive               21.4               6              258
#> Hornet Sportabout            18.7               8              360
#> Valiant                      18.1               6              225

Created on 2024-11-21 with reprex v2.1.1

@strengejacke
Copy link
Member Author

@DominiqueMakowski @easystats/core-team any suggestions for further patterns?

@strengejacke strengejacke marked this pull request as ready for review November 21, 2024 21:39
@strengejacke
Copy link
Member Author

the many rd-files are just due to lintr-updates, can be ignored when reviewing this PR.

@strengejacke strengejacke changed the title Allow curl-styled pattern for data_rename() Allow glue-styled pattern for data_rename() Nov 22, 2024
@strengejacke
Copy link
Member Author

I added some more options:

library(datawizard)
data(mtcars)
out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "formerly_{col}")
head(out)
#>                   formerly_mpg formerly_cyl formerly_disp
#> Mazda RX4                 21.0            6           160
#> Mazda RX4 Wag             21.0            6           160
#> Datsun 710                22.8            4           108
#> Hornet 4 Drive            21.4            6           258
#> Hornet Sportabout         18.7            8           360
#> Valiant                   18.1            6           225

out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "{col}_is_column_{n}")
head(out)
#>                   mpg_is_column_1 cyl_is_column_2 disp_is_column_3
#> Mazda RX4                    21.0               6              160
#> Mazda RX4 Wag                21.0               6              160
#> Datsun 710                   22.8               4              108
#> Hornet 4 Drive               21.4               6              258
#> Hornet Sportabout            18.7               8              360
#> Valiant                      18.1               6              225

out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "new_{letter}")
head(out)
#>                   new_a new_b new_c
#> Mazda RX4          21.0     6   160
#> Mazda RX4 Wag      21.0     6   160
#> Datsun 710         22.8     4   108
#> Hornet 4 Drive     21.4     6   258
#> Hornet Sportabout  18.7     8   360
#> Valiant            18.1     6   225

x <- c("hi", "there", "!")
out <- data_rename(mtcars[1:3], c("mpg", "cyl", "disp"), "col_{x}")
head(out)
#>                   col_hi col_there col_!
#> Mazda RX4           21.0         6   160
#> Mazda RX4 Wag       21.0         6   160
#> Datsun 710          22.8         4   108
#> Hornet 4 Drive      21.4         6   258
#> Hornet Sportabout   18.7         8   360
#> Valiant             18.1         6   225

data_rename(mtcars[1:3], c("mpg", "disp"), "col_{x}")
#> Error: The number of values provided in `{x}` (3 values) do not match the
#>   number of the columns to rename (2 columns).

Created on 2024-11-24 with reprex v2.1.1

@strengejacke
Copy link
Member Author

Rendering of help page:

Positron

image

RStudio

image

R/data_rename.R Outdated Show resolved Hide resolved
R/data_rename.R Show resolved Hide resolved
R/data_rename.R Outdated Show resolved Hide resolved
R/data_rename.R Outdated Show resolved Hide resolved
R/data_rename.R Show resolved Hide resolved
tests/testthat/test-data_rename.R Show resolved Hide resolved
R/data_rename.R Outdated Show resolved Hide resolved
Copy link
Member

@etiennebacher etiennebacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@strengejacke strengejacke merged commit b727cb6 into main Nov 27, 2024
22 checks passed
@strengejacke strengejacke deleted the curl_styled_rename branch November 27, 2024 14:52
@bwiernik
Copy link
Contributor

Could we use .col or .n or .letter instead? I regularly see data frames with names "col" and "n" and am worried about potential collisions. Would also be consistent with tidyverse naming conventions where "special" column names/pronouns start with .

@strengejacke
Copy link
Member Author

I don't think it matters because it's only recognized when inside {}. You usually don't have column names like {n}_times, but rather n_times.

@strengejacke
Copy link
Member Author

I think it's even less problematic: the {} tokens are placeholders in the rename argument and don't refer to columns that should be selected, so we don't run into any problems here, or am I missing something?

@bwiernik
Copy link
Contributor

I wasn't sure how you had implemented the {} and whether it was evaluating the names inside {} (like glue does)

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

Successfully merging this pull request may close these issues.

3 participants