Skip to content

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

@wkdavis

Description

@wkdavis

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.

Metadata

Metadata

Labels

bugan unexpected problem or unintended behavior

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions