Skip to content

Select column to be last #3051

@jrosen48

Description

@jrosen48

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:

library(dplyr)
iris <- tbl_df(iris)
iris <- select(iris, everything(), Sepal.Length)

Here is the output:

> iris
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl>  <fctr>
 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
 7          4.6         3.4          1.4         0.3  setosa
 8          5.0         3.4          1.5         0.2  setosa
 9          4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa
# ... with 140 more rows

There is the following work-around given in an answer to this Stack Overflow question, however this seems counter-intuitive:

iris <- select(iris, everything(), -Sepal.Length, Sepal.Length)

Here is the output:

> iris
# A tibble: 150 x 5
   Sepal.Width Petal.Length Petal.Width Species Sepal.Length
         <dbl>        <dbl>       <dbl>  <fctr>        <dbl>
 1         3.5          1.4         0.2  setosa          5.1
 2         3.0          1.4         0.2  setosa          4.9
 3         3.2          1.3         0.2  setosa          4.7
 4         3.1          1.5         0.2  setosa          4.6
 5         3.6          1.4         0.2  setosa          5.0
 6         3.9          1.7         0.4  setosa          5.4
 7         3.4          1.4         0.3  setosa          4.6
 8         3.4          1.5         0.2  setosa          5.0
 9         2.9          1.4         0.2  setosa          4.4
10         3.1          1.5         0.1  setosa          4.9
# ... with 140 more rows

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions