Sort columns of a dataframe by column name

RSortingDataset

R Problem Overview


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.

R Solutions


Solution 1 - R

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.

Solution 2 - R

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

test %>% 
    select(sort(names(.)))

Solution 3 - R

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")]

Solution 4 - R

  test[,sort(names(test))]

sort on names of columns can work easily.

Solution 5 - R

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 V11

Solution 6 - R

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())

Solution 7 - R

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")))]

Solution 8 - R

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())

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

Solution 9 - R

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

sort(colnames(test[1:ncol(test)] ))

Solution 10 - R

another option is..

mtcars %>% dplyr::select(order(names(mtcars)))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn ClarkView Question on Stackoverflow
Solution 1 - RJamesView Answer on Stackoverflow
Solution 2 - RAndrew BrēzaView Answer on Stackoverflow
Solution 3 - RMANOJ KUMARView Answer on Stackoverflow
Solution 4 - RShalini BaranwalView Answer on Stackoverflow
Solution 5 - RdemarsylvainView Answer on Stackoverflow
Solution 6 - RXavier Jiménez AlbánView Answer on Stackoverflow
Solution 7 - RTom WagstaffView Answer on Stackoverflow
Solution 8 - RBritView Answer on Stackoverflow
Solution 9 - RKNNView Answer on Stackoverflow
Solution 10 - RAntexView Answer on Stackoverflow