3

I want to automatically sort the data frame based on the last column name. Since the last column name in my data frame will be dynamic, I cannot specify the column name.

Below is what i want to achieve as an output.

iris %>%  
  select(-Species) %>% 
  arrange(desc(Petal.Width))

As suggested in response i tried below option, however it dosen't work. Am i missing something?

iris %>%  
  select(-Species) %>%  
  arrange(desc(ncol(.)))


saura
  • 41
  • 2
  • Thank you. Tried to compare the outputs from two examples, however your suggestion doesn't seem to work. Am i missing something? iris %>% select(-Species) %>% arrange(desc(Petal.Width)) and iris %>% select(-Species) %>% arrange(desc(ncol(.))) – saura Aug 07 '22 at 15:00
  • you should edit your question with the differences you're seeing – gaut Aug 07 '22 at 15:01
  • Try this: `iris %>% arrange(desc(tail(colnames(.), 1)))` – TarJae Aug 07 '22 at 15:13
  • Tried the below code but it dosent seem to sort the last column: iris %>% select(-Species) %>% arrange(desc(tail(colnames(.), 1))) – saura Aug 07 '22 at 15:15

3 Answers3

2

last_col() is only supported in functions that feature tidy selection syntax, which arrange()doesn’t.

ncol() will give you the number of the last column, we can use it to subset the data.frame.

See TarJae‘s answer for another Dplyr option to make tidy selection syntax available inside arrange().

library(dplyr)

iris %>%
  select(!Species) %>%
  arrange(desc(.[,ncol(.)]))
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1            6.3         3.3          6.0         2.5
#> 2            7.2         3.6          6.1         2.5
#> 3            6.7         3.3          5.7         2.5
#> 4            5.8         2.8          5.1         2.4
#> 5            6.3         3.4          5.6         2.4
#> 6            6.7         3.1          5.6         2.4
#> 7            6.4         3.2          5.3         2.3
#> 8            7.7         2.6          6.9         2.3
#> 9            6.9         3.2          5.7         2.3
#> 10           7.7         3.0          6.1         2.3
#> 11           6.9         3.1          5.1         2.3
#> 12           6.8         3.2          5.9         2.3
#> 13           6.7         3.0          5.2         2.3
#> 14           6.2         3.4          5.4         2.3
#> 15           6.5         3.0          5.8         2.2
#> 16           7.7         3.8          6.7         2.2
#> 17           6.4         2.8          5.6         2.2
#> 18           7.1         3.0          5.9         2.1
#> 19           7.6         3.0          6.6         2.1
#> 20           6.8         3.0          5.5         2.1
#> 21           6.7         3.3          5.7         2.1
#> 22           6.4         2.8          5.6         2.1
#> 23           6.9         3.1          5.4         2.1
#> 24           6.5         3.2          5.1         2.0
#> 25           5.7         2.5          5.0         2.0
#> 26           5.6         2.8          4.9         2.0
#> 27           7.7         2.8          6.7         2.0
#> 28           7.9         3.8          6.4         2.0
#> 29           6.5         3.0          5.2         2.0
#> 30           5.8         2.7          5.1         1.9
#> 31           6.4         2.7          5.3         1.9
#> 32           7.4         2.8          6.1         1.9
#> 33           5.8         2.7          5.1         1.9
#> 34           6.3         2.5          5.0         1.9
#> 35           5.9         3.2          4.8         1.8
#> 36           6.3         2.9          5.6         1.8
#> 37           7.3         2.9          6.3         1.8
#> 38           6.7         2.5          5.8         1.8
#> 39           6.5         3.0          5.5         1.8
#> 40           6.3         2.7          4.9         1.8
#> 41           7.2         3.2          6.0         1.8
#> 42           6.2         2.8          4.8         1.8
#> 43           6.1         3.0          4.9         1.8
#> 44           6.4         3.1          5.5         1.8
#> 45           6.0         3.0          4.8         1.8
#> 46           5.9         3.0          5.1         1.8
#> 47           6.7         3.0          5.0         1.7
#> 48           4.9         2.5          4.5         1.7
#> 49           6.3         3.3          4.7         1.6
#> 50           6.0         2.7          5.1         1.6
#> 51           6.0         3.4          4.5         1.6
#> 52           7.2         3.0          5.8         1.6
#> 53           6.4         3.2          4.5         1.5
#> 54           6.9         3.1          4.9         1.5
#> 55           6.5         2.8          4.6         1.5
#> 56           5.9         3.0          4.2         1.5
#> 57           5.6         3.0          4.5         1.5
#> 58           6.2         2.2          4.5         1.5
#> 59           6.3         2.5          4.9         1.5
#> 60           6.0         2.9          4.5         1.5
#> 61           5.4         3.0          4.5         1.5
#> 62           6.7         3.1          4.7         1.5
#> 63           6.0         2.2          5.0         1.5
#> 64           6.3         2.8          5.1         1.5
#> 65           7.0         3.2          4.7         1.4
#> 66           5.2         2.7          3.9         1.4
#> 67           6.1         2.9          4.7         1.4
#> 68           6.7         3.1          4.4         1.4
#> 69           6.6         3.0          4.4         1.4
#> 70           6.8         2.8          4.8         1.4
#> 71           6.1         3.0          4.6         1.4
#> 72           6.1         2.6          5.6         1.4
#> 73           5.5         2.3          4.0         1.3
#> 74           5.7         2.8          4.5         1.3
#> 75           6.6         2.9          4.6         1.3
#> 76           5.6         2.9          3.6         1.3
#> 77           6.1         2.8          4.0         1.3
#> 78           6.4         2.9          4.3         1.3
#> 79           6.3         2.3          4.4         1.3
#> 80           5.6         3.0          4.1         1.3
#> 81           5.5         2.5          4.0         1.3
#> 82           5.6         2.7          4.2         1.3
#> 83           5.7         2.9          4.2         1.3
#> 84           6.2         2.9          4.3         1.3
#> 85           5.7         2.8          4.1         1.3
#> 86           6.1         2.8          4.7         1.2
#> 87           5.8         2.7          3.9         1.2
#> 88           5.5         2.6          4.4         1.2
#> 89           5.8         2.6          4.0         1.2
#> 90           5.7         3.0          4.2         1.2
#> 91           5.6         2.5          3.9         1.1
#> 92           5.5         2.4          3.8         1.1
#> 93           5.1         2.5          3.0         1.1
#> 94           4.9         2.4          3.3         1.0
#> 95           5.0         2.0          3.5         1.0
#> 96           6.0         2.2          4.0         1.0
#> 97           5.8         2.7          4.1         1.0
#> 98           5.7         2.6          3.5         1.0
#> 99           5.5         2.4          3.7         1.0
#> 100          5.0         2.3          3.3         1.0
#> 101          5.0         3.5          1.6         0.6
#> 102          5.1         3.3          1.7         0.5
#> 103          5.4         3.9          1.7         0.4
#> 104          5.7         4.4          1.5         0.4
#> 105          5.4         3.9          1.3         0.4
#> 106          5.1         3.7          1.5         0.4
#> 107          5.0         3.4          1.6         0.4
#> 108          5.4         3.4          1.5         0.4
#> 109          5.1         3.8          1.9         0.4
#> 110          4.6         3.4          1.4         0.3
#> 111          5.1         3.5          1.4         0.3
#> 112          5.7         3.8          1.7         0.3
#> 113          5.1         3.8          1.5         0.3
#> 114          5.0         3.5          1.3         0.3
#> 115          4.5         2.3          1.3         0.3
#> 116          4.8         3.0          1.4         0.3
#> 117          5.1         3.5          1.4         0.2
#> 118          4.9         3.0          1.4         0.2
#> 119          4.7         3.2          1.3         0.2
#> 120          4.6         3.1          1.5         0.2
#> 121          5.0         3.6          1.4         0.2
#> 122          5.0         3.4          1.5         0.2
#> 123          4.4         2.9          1.4         0.2
#> 124          5.4         3.7          1.5         0.2
#> 125          4.8         3.4          1.6         0.2
#> 126          5.8         4.0          1.2         0.2
#> 127          5.4         3.4          1.7         0.2
#> 128          4.6         3.6          1.0         0.2
#> 129          4.8         3.4          1.9         0.2
#> 130          5.0         3.0          1.6         0.2
#> 131          5.2         3.5          1.5         0.2
#> 132          5.2         3.4          1.4         0.2
#> 133          4.7         3.2          1.6         0.2
#> 134          4.8         3.1          1.6         0.2
#> 135          5.5         4.2          1.4         0.2
#> 136          4.9         3.1          1.5         0.2
#> 137          5.0         3.2          1.2         0.2
#> 138          5.5         3.5          1.3         0.2
#> 139          4.4         3.0          1.3         0.2
#> 140          5.1         3.4          1.5         0.2
#> 141          4.4         3.2          1.3         0.2
#> 142          5.1         3.8          1.6         0.2
#> 143          4.6         3.2          1.4         0.2
#> 144          5.3         3.7          1.5         0.2
#> 145          5.0         3.3          1.4         0.2
#> 146          4.9         3.1          1.5         0.1
#> 147          4.8         3.0          1.4         0.1
#> 148          4.3         3.0          1.1         0.1
#> 149          5.2         4.1          1.5         0.1
#> 150          4.9         3.6          1.4         0.1

Created on 2022-08-07 by the reprex package (v2.0.1)

TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
2

We could do it using across:

library(dplyr)
library(psych) # for `headTail()`

iris %>%  
  select(-Species) %>%  
  arrange(across(last_col(), desc)) %>% 
  headTail()
    Sepal.Length Sepal.Width Petal.Length Petal.Width
1            6.3         3.3            6         2.5
2            7.2         3.6          6.1         2.5
3            6.7         3.3          5.7         2.5
4            5.8         2.8          5.1         2.4
...          ...         ...          ...         ...
147          4.8           3          1.4         0.1
148          4.3           3          1.1         0.1
149          5.2         4.1          1.5         0.1
150          4.9         3.6          1.4         0.1

TarJae
  • 72,363
  • 6
  • 19
  • 66
0

Using Base R option

subset(iris , select = -Species) |>
      (\(x) x[order(- x[ncol(x)]) , ])()
  • output
   Sepal.Length Sepal.Width Petal.Length Petal.Width
101          6.3         3.3          6.0         2.5
110          7.2         3.6          6.1         2.5
145          6.7         3.3          5.7         2.5
115          5.8         2.8          5.1         2.4
137          6.3         3.4          5.6         2.4
141          6.7         3.1          5.6         2.4
116          6.4         3.2          5.3         2.3
119          7.7         2.6          6.9         2.3
......................................................
40           5.1         3.4          1.5         0.2
43           4.4         3.2          1.3         0.2
47           5.1         3.8          1.6         0.2
48           4.6         3.2          1.4         0.2
49           5.3         3.7          1.5         0.2
50           5.0         3.3          1.4         0.2
10           4.9         3.1          1.5         0.1
13           4.8         3.0          1.4         0.1
14           4.3         3.0          1.1         0.1
33           5.2         4.1          1.5         0.1
38           4.9         3.6          1.4         0.1
Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19