Skip to content

Transform a vector into a format that will be sorted in descending order. This is useful within arrange().

Usage

desc(x)

Arguments

x

vector to transform

Examples

desc(1:10)
#>  [1]  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10
desc(factor(letters))
#>  [1]  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10 -11 -12 -13 -14 -15 -16 -17
#> [18] -18 -19 -20 -21 -22 -23 -24 -25 -26

first_day <- seq(as.Date("1910/1/1"), as.Date("1920/1/1"), "years")
desc(first_day)
#>  [1] 21915 21550 21185 20819 20454 20089 19724 19358 18993 18628 18263

starwars %>% arrange(desc(mass))
#> # A tibble: 87 × 14
#>    name  height  mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex   gender homew…⁵
#>    <chr>  <int> <dbl> <chr>   <chr>   <chr>     <dbl> <chr> <chr>  <chr>  
#>  1 Jabb…    175  1358 NA      green-… orange    600   herm… mascu… Nal Hu…
#>  2 Grie…    216   159 none    brown,… green,…    NA   male  mascu… Kalee  
#>  3 IG-88    200   140 none    metal   red        15   none  mascu… NA     
#>  4 Dart…    202   136 none    white   yellow     41.9 male  mascu… Tatooi…
#>  5 Tarf…    234   136 brown   brown   blue       NA   male  mascu… Kashyy…
#>  6 Owen…    178   120 brown,… light   blue       52   male  mascu… Tatooi…
#>  7 Bossk    190   113 none    green   red        53   male  mascu… Trando…
#>  8 Chew…    228   112 brown   unknown blue      200   male  mascu… Kashyy…
#>  9 Jek …    180   110 brown   fair    blue       NA   male  mascu… Bestin…
#> 10 Dext…    198   102 none    brown   yellow     NA   male  mascu… Ojom   
#> # … with 77 more rows, 4 more variables: species <chr>, films <list>,
#> #   vehicles <list>, starships <list>, and abbreviated variable names
#> #   ¹​hair_color, ²​skin_color, ³​eye_color, ⁴​birth_year, ⁵​homeworld