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

Select and relocate work inconsistenly on an empty dataframe #6167

Closed
jttoivon opened this issue Jan 28, 2022 · 1 comment · Fixed by #6342
Closed

Select and relocate work inconsistenly on an empty dataframe #6167

jttoivon opened this issue Jan 28, 2022 · 1 comment · Fixed by #6342
Labels
bug an unexpected problem or unintended behavior columns ↔️ Operations on columns: mutate(), select(), rename(), relocate()

Comments

@jttoivon
Copy link

I expect that trying to relocate a non-existing column with any_of would return the original dataframe. Even if the dataframe is empty. The function select seems to work more consistently with any_of.

My dplyr version is 1.0.7.

library(tidyverse)
tibble(a=1:2) %>% relocate(any_of("b"))
#> # A tibble: 2 × 1
#>       a
#>   <int>
#> 1     1
#> 2     2
tibble() %>% relocate(any_of("b"))
#> Error: Can't subset columns that don't exist.
#> x Location 1 doesn't exist.
#> ℹ There are only 0 columns.
tibble() %>% select(any_of("b"))
#> # A tibble: 0 × 0

Created on 2022-01-28 by the reprex package (v2.0.1)

@DavisVaughan
Copy link
Member

This does seem like a bug. Some related issues with .before and .after:

library(tidyverse)

tibble(x = 1) %>% relocate(x, .before = any_of("a"))
#> Warning in min(unname(tidyselect::eval_select(.before, .data))): no non-missing
#> arguments to min; returning Inf
#> Error in seq.int(from, to): 'to' must be a finite number
tibble(x = 1) %>% relocate(x, .after = any_of("a"))
#> Warning in max(unname(tidyselect::eval_select(.after, .data))): no non-missing
#> arguments to max; returning -Inf
#> Error in seq.int(from, to): 'from' must be a finite number

Created on 2022-01-28 by the reprex package (v2.0.1)

I think we need to slightly tweak the logic of relocate() to better handle the case of empty selections. .before = <empty-selection> should probably be the same as .before = NULL, same with .after. This means if want to relocate columns before/after "nothing" then they end up at the front of the data frame, which is consistent with what relocate(data, cols) does.

@romainfrancois romainfrancois added the bug an unexpected problem or unintended behavior label Feb 4, 2022
@hadley hadley added the columns ↔️ Operations on columns: mutate(), select(), rename(), relocate() label Apr 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior columns ↔️ Operations on columns: mutate(), select(), rename(), relocate()
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants