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

Differing behavior when vars doesn't match any columns #4324

Closed
wkdavis opened this issue Apr 9, 2019 · 2 comments
Closed

Differing behavior when vars doesn't match any columns #4324

wkdavis opened this issue Apr 9, 2019 · 2 comments
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@wkdavis
Copy link

wkdavis commented Apr 9, 2019

It appears that the behavior of mutate_at() and rename_at() differs when no columns are returned using vars() and contains().

For mutate_at(), when no vars are returned (no columns are matched) it returns an unmodified tbl.

mtcars %>% mutate_at(vars(contains("fake_col")),~paste0("NewCol.",.))

    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4

However, for rename_at() when no vars are returned (no columns are matched) an error is thrown.

mtcars %>% rename_at(vars(contains("fake_col")),~paste0("NewCol.",.))

#> Error: `nm` must be `NULL` or a character vector the same length as `x`

This might be the desired behavior (if so, I will close this issue). I was relying on the mutate_at() behavior with the implied conditional that if nothing matches vars(contains()) the piped sequence could continue with the unmodified tbl, and then I ran into trouble when I was counting on rename_at() to behave the same way.

@wkdavis wkdavis changed the title Differing behavior when vars doesn't match any columns Differing behavior when vars doesn't match any columns Apr 9, 2019
@romainfrancois romainfrancois self-assigned this Apr 11, 2019
@romainfrancois romainfrancois added the bug an unexpected problem or unintended behavior label Apr 11, 2019
@romainfrancois romainfrancois added this to the 0.8.2 milestone Apr 11, 2019
romainfrancois added a commit that referenced this issue Apr 11, 2019
@wkdavis
Copy link
Author

wkdavis commented Apr 11, 2019

@romainfrancois select_at() throws a similar error to rename_at().

> mtcars %>% select_at(vars(contains("fake_col")),~paste0("NewCol.",.))
Error: `nm` must be `NULL` or a character vector the same length as `x`

In the case of select_at() it may be more intuitive to return an empty tbl (rather than .tbl unmodified), as you are effectively selecting no columns because none of the columns matched the .predicate. But after thinking about it for a moment, this might be unexpected considering that the behavior of mutate_at() (and now rename_at()) is to return an unmodified tbl upon not matching any .vars.

@lock
Copy link

lock bot commented Nov 2, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Nov 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants