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

Don't error when dropping nonexistent columns #345

Closed
chrsigg opened this issue Jun 6, 2023 · 2 comments
Closed

Don't error when dropping nonexistent columns #345

chrsigg opened this issue Jun 6, 2023 · 2 comments

Comments

@chrsigg
Copy link

chrsigg commented Jun 6, 2023

Dropping a named column that does not exist in the table results in an error:

library(tidyverse)
my <- tibble(a = 1, b = 2)
my %>% select(-c)
#> Error in `select()`:
#> ! Can't subset columns that don't exist.
#> ✖ Column `c` doesn't exist.
#> Backtrace:
#>      ▆
#>   1. ├─my %>% select(-c)
#>   2. ├─dplyr::select(., -c)
#>   3. ├─dplyr:::select.data.frame(., -c)
#>   4. │ └─tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#>   5. │   └─tidyselect:::eval_select_impl(...)
...

Created on 2023-06-06 with reprex v2.0.2

But arguably, there is no need for this error, returning the unmodified table instead would have given the correct result.

With a more tolerant behavior, code that wants to ensure that certain columns are not present in a table would no longer have to check first which column names are currently present.

@lionel-
Copy link
Member

lionel- commented Jun 6, 2023

We're unlikely to make this less strict but you can use select(-any_of("c")) to explicitly get the tolerant behaviour.

@chrsigg
Copy link
Author

chrsigg commented Jun 6, 2023

Thank you, I missed that in the documentation.

@chrsigg chrsigg closed this as completed Jun 6, 2023
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