Sorting columns of a data frame by column name

This may be a simple question, but I don't know how to sort the 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)) # CAB # 1 0 4 1 # 2 2 2 3 # 3 4 4 8 # 4 7 7 3 # 5 8 8 2 

I like to order columns by column names in alphabetical order to achieve

 # ABC # 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 specific order:

 # BAC # 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 10,000 variables. Therefore, the process should be more automated.

+70
sorting r dataset
Sep 07 '11 at 13:27
source share
9 answers

You can use order in names and use this to arrange columns with a subset:

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

For your own specific order, you will need to define your own name mapping for the order. It will depend on how you would like to do this, but replacing any function with the one with order above should give the desired result.

You can, for example, see Order lines of a data frame in accordance with the target vector, which sets the desired order , i.e. you can match your names data frame against the target vector containing the desired column order.

+109
07 Sep 2018-11-11T00:
source share
 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 following simple replacement function can be performed (but only if there are not many columns in the data frame):

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

For others:

 test <- test[, c("B", "A", "C")] 
+13
Apr 01 '14 at 20:04
source share

Here is dplyr obligatory answer in case someone wants to do this with a handset.

 test %>% select(sort(names(.))) 
+10
Aug 16 '18 at 20:57
source share
  test[,sort(names(test))] 

sorting by column names can easily work.

+4
Nov 18 '17 at 18:09
source share

If you need only one or several columns in front and you don’t care about the order of the rest:

 require(dplyr) test %>% select(B, everything()) 
+1
Oct 27 '18 at 10:12
source share

Here is what I found to achieve a similar problem with my dataset.

First, do what I mentioned above, for example,

 test[ , order(names(test))] 

Secondly, use the all () function in dplyr to move certain columns of interest (for example, "D", "G", "K") at the beginning of the data frame, placing the alphabetically ordered columns after these,

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

0
Jul 25 '17 at 16:51
source share

Similar to the other syntax above, but for training - can you sort by column names?

 sort(colnames(test[1:ncol(test)] )) 
0
Mar 15 '19 at 20:32
source share

Another option is to use str_sort() from the stringr library with argument numeric = TRUE

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

# [1] V1 V3 V11

0
May 19 '19 at 8:27
source share

So that a specific column appears first, and then alphabetically, I would suggest the following solution:

 test[, c("myFirstColumn", sort(setdiff(names(test), "myFirstColumn")))] 
0
Jun 28 '19 at 15:03
source share



All Articles