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 named character vector in select, to rename variables #512

Merged
merged 13 commits into from
Jun 8, 2024

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Jun 7, 2024

library(datawizard)
data(mtcars)

data_select(mtcars, c(neu = "mpg", alt = "cyl", hoho = "wt")) |> head()
#>                    neu alt  hoho
#> Mazda RX4         21.0   6 2.620
#> Mazda RX4 Wag     21.0   6 2.875
#> Datsun 710        22.8   4 2.320
#> Hornet 4 Drive    21.4   6 3.215
#> Hornet Sportabout 18.7   8 3.440
#> Valiant           18.1   6 3.460
data_select(mtcars, c(neu = "mpg", "cyl", hoho = "wt")) |> head()
#>                    neu cyl  hoho
#> Mazda RX4         21.0   6 2.620
#> Mazda RX4 Wag     21.0   6 2.875
#> Datsun 710        22.8   4 2.320
#> Hornet 4 Drive    21.4   6 3.215
#> Hornet Sportabout 18.7   8 3.440
#> Valiant           18.1   6 3.460

Created on 2024-06-07 with reprex v2.1.0

NEWS.md Outdated Show resolved Hide resolved
R/select_nse.R Outdated Show resolved Hide resolved
R/select_nse.R Outdated Show resolved Hide resolved
tests/testthat/test-data_select.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, can you also add tests for:

  • what happens when the new name already exists in the data?
  • what happens if the new name is used in exclude?

@strengejacke
Copy link
Member Author

what happens when the new name already exists in the data?

It can't already exist in the data, because only the selected variables are returned. Thus, the case would be that a new name is chosen twice or more often. I'd say this should give an error?

what happens if the new name is used in exclude?

select and exclude are processed before we check names, so any names used in exclude will be ignored for now (it also wouldn't make sense to specify a value in exclude that matches a new name, so the selected variable will be excluded, too - at least it wouldn't make sense in interactive use).

What would you suggest? Check earlier and also exclude those variables?

@etiennebacher
Copy link
Member

I'd say this should give an error?

Agreed, I'm fine with the current changes

What would you suggest? Check earlier and also exclude those variables?

My second point was more about whether the following works, and it does:

library(datawizard)

data_select(head(mtcars), c(drat = "mpg"), exclude = "drat")
#>                   drat
#> Mazda RX4         21.0
#> Mazda RX4 Wag     21.0
#> Datsun 710        22.8
#> Hornet 4 Drive    21.4
#> Hornet Sportabout 18.7
#> Valiant           18.1

We could add a test for this situation (if you agree with the current behavior)

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.

Apart from adding a test as mentioned in my comment above, this looks good to me, thanks!

R/select_nse.R Outdated Show resolved Hide resolved
tests/testthat/test-data_select.R Outdated Show resolved Hide resolved
@strengejacke strengejacke merged commit 7db36d6 into main Jun 8, 2024
23 of 29 checks passed
@strengejacke strengejacke deleted the rename_in_data_select branch June 8, 2024 16:25
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.

2 participants