How to sort columns alphabetically

In this tutorial, we will learn how to sort columns of a dataframe in alphabetical order. We will first use tidyverse to sort columns of a dataframe using two different ways. And then we will see how to use base R functions to sort the columns.

Let us load tidyverse.

library(tidyverse)

We will use starwars that is built-in with dplyr package. Let us look at the column names of starwars dataframe.

starwars %>% colnames()

 [1] "name"       "height"     "mass"       "hair_color" "skin_color"
 [6] "eye_color"  "birth_year" "sex"        "gender"     "homeworld" 
[11] "species"    "films"      "vehicles"   "starships" 

Sorting columns with tidyverse

Using order() function on the column names we can order the column names in alphabatical order. This gives us a vector with ordered column names and we can use select() function from dplyr to select the ordered columns

starwars %>% 
  select(order(colnames(.))) 

# A tibble: 87 × 14
   birth_year eye_color films     gender hair_color height homeworld  mass name 
        <dbl> <chr>     <list>    <chr>  <chr>       <int> <chr>     <dbl> <chr>
 1       19   blue      <chr [5]> mascu… blond         172 Tatooine     77 Luke…
 2      112   yellow    <chr [6]> mascu… <NA>          167 Tatooine     75 C-3PO
 3       33   red       <chr [7]> mascu… <NA>           96 Naboo        32 R2-D2
 4       41.9 yellow    <chr [4]> mascu… none          202 Tatooine    136 Dart…
 5       19   brown     <chr [5]> femin… brown         150 Alderaan     49 Leia…
 6       52   blue      <chr [3]> mascu… brown, gr…    178 Tatooine    120 Owen…
 7       47   blue      <chr [3]> femin… brown         165 Tatooine     75 Beru…
 8       NA   red       <chr [1]> mascu… <NA>           97 Tatooine     32 R5-D4
 9       24   brown     <chr [1]> mascu… black         183 Tatooine     84 Bigg…
10       57   blue-gray <chr [6]> mascu… auburn, w…    182 Stewjon      77 Obi-…
# … with 77 more rows, and 5 more variables: sex <chr>, skin_color <chr>,
#   species <chr>, starships <list>, vehicles <list>

We can see that after ordering the column names are alphabetical order.

starwars %>% 
  select(order(colnames(.))) %>%
  colnames()

 [1] "birth_year" "eye_color"  "films"      "gender"     "hair_color"
 [6] "height"     "homeworld"  "mass"       "name"       "sex"       
[11] "skin_color" "species"    "starships"  "vehicles"  

Sorting columns using tidyselect’s peek_vars()

Another approach to sort columns using tidyverse is to use tidyselect’s peek_vars() function with sort() function as shown below.

starwars %>% 
  select(sort(tidyselect::peek_vars())) %>%
  colnames()

 [1] "birth_year" "eye_color"  "films"      "gender"     "hair_color"
 [6] "height"     "homeworld"  "mass"       "name"       "sex"       
[11] "skin_color" "species"    "starships"  "vehicles"  

Sorting columns using base R

We can also sort the columns using base R funntions alone. Here we use order() function on the column names and use [] to select the ordered columns.

starwars[, order(colnames(starwars))]

# A tibble: 87 × 14
   birth_year eye_color films     gender hair_color height homeworld  mass name 
        <dbl> <chr>     <list>    <chr>  <chr>       <int> <chr>     <dbl> <chr>
 1       19   blue      <chr [5]> mascu… blond         172 Tatooine     77 Luke…
 2      112   yellow    <chr [6]> mascu… <NA>          167 Tatooine     75 C-3PO
 3       33   red       <chr [7]> mascu… <NA>           96 Naboo        32 R2-D2
 4       41.9 yellow    <chr [4]> mascu… none          202 Tatooine    136 Dart…
 5       19   brown     <chr [5]> femin… brown         150 Alderaan     49 Leia…
 6       52   blue      <chr [3]> mascu… brown, gr…    178 Tatooine    120 Owen…
 7       47   blue      <chr [3]> femin… brown         165 Tatooine     75 Beru…
 8       NA   red       <chr [1]> mascu… <NA>           97 Tatooine     32 R5-D4
 9       24   brown     <chr [1]> mascu… black         183 Tatooine     84 Bigg…
10       57   blue-gray <chr [6]> mascu… auburn, w…    182 Stewjon      77 Obi-…
# … with 77 more rows, and 5 more variables: sex <chr>, skin_color <chr>,
#   species <chr>, starships <list>, vehicles <list>
starwars[, order(colnames(starwars))] %>% colnames()

 [1] "birth_year" "eye_color"  "films"      "gender"     "hair_color"
 [6] "height"     "homeworld"  "mass"       "name"       "sex"       
[11] "skin_color" "species"    "starships"  "vehicles"  

https://stackoverflow.com/questions/29873293/dply-order-columns-alphabetically-in-r

Exit mobile version