-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
dplyr::rename with fixed new-names and old-names given by length-1 named vectors gives wrong names #5207
Comments
With the pr #5224, you'd get: library(dplyr, warn.conflicts = FALSE)
standardize <- function(df, fooName) {
dplyr::rename(df, foo = !!fooName)
}
standardize(data.frame(bar = 1), "bar")
#> foo
#> 1 1
name <- c(buzz = "bar")
standardize(data.frame(bar = 1), name)
#> buzz
#> 1 1 Created on 2020-05-11 by the reprex package (v0.3.0) but I'm not sure this is what's expected |
It is not. If the desired name ( For the record, I only defined |
I think by default it should be an error to supply both outer and inner names. The problem with letting the outer name override the names of the character vector is that it isn't generalisable when the vector is length > 1. So the proper syntax would be |
I ran into similar issues when using named vectors with Both methods make use (or want to) of the names of vector, while I would expect them to just use the actual values. Here is an example to demonstrate (using dplyr version 1.0.0): # Example data frame and function
my_data <- data.frame(A = 1:3, B = c("a", "b", "c"), C = 7:9)
my_fun <- function(x){x+1}
# Selecting columns with a normal vector
my_cols <- c("A", "C")
dplyr::mutate_at(my_data, .vars = dplyr::all_of(my_cols), .funs = my_fun)
#> A B C
#> 1 2 a 8
#> 2 3 b 9
#> 3 4 c 10
dplyr::mutate(my_data, dplyr::across(.cols = dplyr::all_of(my_cols), .fns = my_fun))
#> A B C
#> 1 2 a 8
#> 2 3 b 9
#> 3 4 c 10
# Selecting columns with a named vector
my_cols <- c(NameA = "A", NameC = "C")
dplyr::mutate_at(my_data, .vars = dplyr::all_of(my_cols), .funs = my_fun)
#> A B C NameA NameC
#> 1 1 a 7 2 8
#> 2 2 b 8 3 9
#> 3 3 c 9 4 10
dplyr::mutate(my_data, dplyr::across(.cols = dplyr::all_of(my_cols), .fns = my_fun))
#> Error: Problem with `mutate()` input `..1`.
#> x object 'NameA' not found
#> i Input `..1` is `dplyr::across(.cols = dplyr::all_of(my_cols), .fns = my_fun)`. Created on 2021-03-23 by the reprex package (v0.3.0) |
#5889 fixes the |
Closing this now, as the library(dplyr, warn.conflicts = FALSE)
dplyr::rename(data.frame(old = 1), new = c(dud = "old"))
#> new...dud
#> 1 1 Created on 2021-05-19 by the reprex package (v2.0.0) |
A one-line reprex of this behavior is:
This is an issue if we have a function where data.frames with myriad names have their names changed to certain "standard" names. In the case below, I have a standard column-name
foo
, but a data.frame's called itbar
instead, so we call the following function to give it the standard nameCreated on 2020-05-07 by the reprex package (v0.3.0)
This works just fine.
However, if, for whatever reason, the value passed to
fooName
is a length-1 named vector, we get the behavior shown at the start:The vector's name is clumsily suffixed to the desired name with an ellipsis. Is this expected behavior? I'm currently solving this by using
foo = unname(fooName)
, which works, but wonder if this is just a bug.The text was updated successfully, but these errors were encountered: