-
Notifications
You must be signed in to change notification settings - Fork 39
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
Inconsistent select() and rename() behavior #90
Comments
From the
Hopefully we can make the error message more informative, though. library(dplyr)
newnames <- c("Sepal.Length", "Petal.Length") %>%
setNames(c("Sepal_Length", "Petal_Length"))
iris %>% select(newnames) %>% names
#> [1] "Sepal_Length" "Petal_Length"
iris %>% rename(newnames) %>% names
#> All arguments must be named
iris %>% select(!!!newnames) %>% names
#> [1] "Sepal_Length" "Petal_Length"
iris %>% rename(!!!newnames) %>% names
#> [1] "Sepal_Length" "Sepal.Width" "Petal_Length" "Petal.Width"
#> [5] "Species"
vars <- list(
Sepal_Length = sym("Sepal.Length"),
Petal_Length = sym("Petal.Length")
)
rename(iris, !!!vars) %>% head()
#> Sepal_Length Sepal.Width Petal_Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa Created on 2019-04-09 by the reprex package (v0.2.1) EDIT For completeness, it also works without library(dplyr)
vars <- c(Sepal_Length = "Sepal.Length", Petal_Length ="Petal.Length")
iris %>% select(!!vars) %>% names
#> [1] "Sepal_Length" "Petal_Length"
iris %>% rename(!!vars) %>% names
#> [1] "Sepal_Length" "Sepal.Width" "Petal_Length" "Petal.Width"
#> [5] "Species" Created on 2019-04-09 by the reprex package (v0.2.1) |
Thanks--though the text you copied is not actually in the help docs? This line is not followed by what you copied:
Am I missing something? Also, it's more the inconsistency that troubles me--why does it work with select, then? The two do share a help page, after all, and I thought they largely shared functionality. |
ping @lionel- , is this a tidyselect issue? |
Yes most likely. |
We're going to deprecate this pattern: iris %>% select(newnames) The correct way to do it will be one of these (first one is recommended): iris %>% select(one_of(newnames))
iris %>% select(!!newnames) The reason is that if the data frame contains a For these reasons, we have decided to make a clear delimitation between expressions referring to columns and expressions referring to the context, like The bottom line is that you need to unquote the character vector: iris %>% rename(!!newnames) |
Passing a named list to select() performs select/rename behavior, but doing the same with rename() produces the confusing error "All arguments must be named"
It does work when "!!!" is applied, but I'm unclear what's different about the structure here, and the message doesn't really provide insight.
The text was updated successfully, but these errors were encountered: