-
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
Select column to be last #3051
Comments
@lionel-: Does this now belong in tidyselect? |
Yes. @hadley we could have The issue is that |
Maybe that can be a new verb, e.g.
Can be used to also move to first:
|
Another useful variant might be cc @jennybc |
This also works: select(iris, -Sepal.Length, Sepal.Length) i.e. select everything except Sepal.Length, then select Sepal.Length. This also works: dplyr::select(iris, -Sepal.Length, everything()) I don't think this is such a common operation that it needs it's own verb. |
Is the logic of selecting a column to be first through If so, then could |
No, it selects everything. It's like supplying |
This functionality seems to be easy enough to implement with existing functionality, and rare enough that it doesn't need its own verb. We could add an example to the documentation, either here or in tidyselect. |
Another take with some tidyeval : library(purrr)
library(rlang)
#>
#> Attaching package: 'rlang'
#> The following objects are masked from 'package:purrr':
#>
#> %@%, %||%, as_function, flatten, flatten_chr, flatten_dbl,
#> flatten_int, flatten_lgl, invoke, list_along, modify, prepend,
#> rep_along, splice
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
back <- function(data, ...){
dots <- quos(...)
ndots <- map(dots, function(q) expr(-!!q) )
select( data, !!!ndots, !!!dots )
}
back(iris, Species) %>% 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
back(iris, Species, starts_with("Petal")) %>% head
#> Sepal.Length Sepal.Width Species Petal.Length Petal.Width
#> 1 5.1 3.5 setosa 1.4 0.2
#> 2 4.9 3.0 setosa 1.4 0.2
#> 3 4.7 3.2 setosa 1.3 0.2
#> 4 4.6 3.1 setosa 1.5 0.2
#> 5 5.0 3.6 setosa 1.4 0.2
#> 6 5.4 3.9 setosa 1.7 0.4 Created on 2018-04-23 by the reprex package (v0.2.0). |
@hadley should we close this as this can easily be done outside of dplyr ? |
Do we want to enhance the documentation first? |
I think one example in the documentation (along the lines of my code above) would be nice. |
- Add documentation example for moving variable to back in `?select` (#3051).
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/ |
Brief description of the problem
Using dplyr, it is easy to select a column to be ordered first (i.e., the first column from the left side), or to order all of the columns, but it seems like there is not a straightforward way to select a column to be ordered last (i.e., the first from the right side).
For example, if I run the following,
Sepal.Length
is still the first column:Here is the output:
There is the following work-around given in an answer to this Stack Overflow question, however this seems counter-intuitive:
Here is the output:
The text was updated successfully, but these errors were encountered: