117

This is possibly a simple question, but I do not know how to order columns alphabetically.

test = data.frame(C = c(0, 2, 4, 7, 8), A = c(4, 2, 4, 7, 8), B = c(1, 3, 8, 3, 2))

#   C A B
# 1 0 4 1
# 2 2 2 3
# 3 4 4 8
# 4 7 7 3
# 5 8 8 2

I like to order the columns by column names alphabetically, to achieve

#   A B C
# 1 4 1 0
# 2 2 3 2
# 3 4 8 4
# 4 7 3 7
# 5 8 2 8

For others I want my own defined order:

#   B A C
# 1 4 1 0
# 2 2 3 2
# 3 4 8 4
# 4 7 3 7
# 5 8 2 8

Please note that my datasets are huge, with 10000 variables. So the process needs to be more automated.

Henrik
  • 65,555
  • 14
  • 143
  • 159
John Clark
  • 2,639
  • 5
  • 19
  • 13

11 Answers11

155

You can use order on the names, and use that to order the columns when subsetting:

test[ , order(names(test))]
  A B C
1 4 1 0
2 2 3 2
3 4 8 4
4 7 3 7
5 8 2 8

For your own defined order, you will need to define your own mapping of the names to the ordering. This would depend on how you would like to do this, but swapping whatever function would to this with order above should give your desired output.

You may for example have a look at Order a data frame's rows according to a target vector that specifies the desired order, i.e. you can match your data frame names against a target vector containing the desired column order.

Community
  • 1
  • 1
James
  • 65,548
  • 14
  • 155
  • 193
  • 6
    To elaborate, test[,c(2,3,1)] or test[,c('A','B','C')] will produce A,B,C column order. The "[" operator is very clever at figuring out what you want to do. – Carl Witthoft Sep 07 '11 at 14:32
  • 3
    thank you, I figured out the second question with help provided; myorder = c("B", "A", "C"), test[,myorder] – John Clark Sep 07 '11 at 14:40
  • Is there a way to sort the columns in a way I want (say C A B)? – TYZ Apr 17 '14 at 19:36
  • You can take advantage of the fact that a data.frame is a list and make it simpler:: `test[ order(names(test)) ]` – ctbrown Sep 19 '16 at 09:25
  • What is the difference between using `names()` vs `colnames()` here? – naco Mar 05 '19 at 23:13
  • 1
    @naco None, read the source of `colnames`: it ends up calling `names` for a `data.frame`. – James Mar 06 '19 at 11:06
  • Is there way to make the ordering numerical? I have var names as "q" then a number 1 - 80 I need ordered and the ordering goes 1, 10, 11... so on which is wrong. I need 1,2 3...so on – Jeff Henderson Aug 18 '20 at 20:26
47

Here's the obligatory dplyr answer in case somebody wants to do this with the pipe.

test %>% 
    select(sort(names(.)))
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
  • 6
    For me this worked well since it's easy to select the variables I want first. Sticking to the original df: `test%>%select(b,sort(names(.)))` will put it as "b,a,c" – Silentdevildoll Sep 30 '19 at 22:43
16
test = data.frame(C=c(0,2,4, 7, 8), A=c(4,2,4, 7, 8), B=c(1, 3, 8,3,2))

Using the simple following function replacement can be performed (but only if data frame does not have many columns):

test <- test[, c("A", "B", "C")]

for others:

test <- test[, c("B", "A", "C")]
Anton K
  • 4,658
  • 2
  • 47
  • 60
MANOJ KUMAR
  • 177
  • 1
  • 3
11

An alternative option is to use str_sort() from library stringr, with the argument numeric = TRUE. This will correctly order column that include numbers not just alphabetically:

str_sort(c("V3", "V1", "V10"), numeric = TRUE)

# [1] V1 V3 V10

demarsylvain
  • 2,103
  • 2
  • 14
  • 33
8
  test[,sort(names(test))]

sort on names of columns can work easily.

Shalini Baranwal
  • 2,780
  • 4
  • 24
  • 34
6

If you only want one or more columns in the front and don't care about the order of the rest:

require(dplyr)
test %>%
  select(B, everything())
4

So to have a specific column come first, then the rest alphabetically, I'd propose this solution:

test[, c("myFirstColumn", sort(setdiff(names(test), "myFirstColumn")))]
Tom Wagstaff
  • 1,443
  • 2
  • 13
  • 15
2

Here is what I found out to achieve a similar problem with my data set.

First, do what James mentioned above, i.e.

test[ , order(names(test))]

Second, use the everything() function in dplyr to move specific columns of interest (e.g., "D", "G", "K") at the beginning of the data frame, putting the alphabetically ordered columns after those ones.

select(test, D, G, K, everything())

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
Brit
  • 169
  • 1
  • 2
1

Similar to other syntax above but for learning - can you sort by column names?

sort(colnames(test[1:ncol(test)] ))
KNN
  • 459
  • 4
  • 19
1

another option is..

mtcars %>% dplyr::select(order(names(mtcars)))
Antex
  • 1,364
  • 4
  • 18
  • 35
0

In data.table you can use the function setcolorder:

setcolorder reorders the columns of data.table, by reference, to the new order provided.

Here a reproducible example:

library(data.table)
test = data.table(C = c(0, 2, 4, 7, 8), A = c(4, 2, 4, 7, 8), B = c(1, 3, 8, 3, 2))
setcolorder(test, c(order(names(test))))
test
#>    A B C
#> 1: 4 1 0
#> 2: 2 3 2
#> 3: 4 8 4
#> 4: 7 3 7
#> 5: 8 2 8

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

Quinten
  • 35,235
  • 5
  • 20
  • 53