Reorder Dataframe based on Character Vector

2019-03-04 17:32发布

问题:

I think this should be really simple, but I cannot find a way to do this.

I want to reorder a dataframe based on a factor. All the answers I can find so far provide logical sorting, but this is arbitrary, and a mixture of numbers and letters. Maybe it's a problem because it is a factor not a vector? But none of the answers for vectors seem to work either.

Any suggestions would be really appreciated!

Example data (note; this data is just for this question, in my real code the dataframe is the output of some other calculations and so I can't just alter the names to something sensible right at the start)

DATA<- data.frame(This=c("120", "60", "90", "OG"), That=c(453, 679,12,252))

DATA

  This That
1  120  453
2   60  679
3   90   12
4   OG  252

I want to sort it in the order 60 - 90 - 120 - OG, i.e.

  This That
1  60   679
2  90   12
3  120  453
4  OG   252

Edit: This is not a duplicate question. As explained above, and in the comment below, the sorting is arbitrary. Lexicographical sorting assumes the row names are inter-related, that isn't the case here. I could have labelled the rows ("unicorn", "18.1", "TREES", "234234235") and wanted them in the order ("234234235", "unicorn", "TREES", "18.1")

回答1:

You can adjust the order with the factor variable as follows:

DATA$This <- factor(DATA$This, levels=c("60", "90", "120", "OG"))

Note that this doesn't change the label values:

DATA
  This That
1  120  453
2   60  679
3   90   12
4   OG  252

But it changes the underlying integers that are mapped to those labels, so that you can order the data as you wanted:

DATA[order(DATA$This),]
  This That
2   60  679
3   90   12
1  120  453
4   OG  252


回答2:

Here is an option for sorting mixed type column, where we convert the column to numeric firstly and sort which makes sure numeric values are always in front of character values and it is by its natural order and then sort by the character type which makes sure the character values are in alphabetical order, assuming This column is of class character already:

library(dplyr)
DATA %>% arrange(as.numeric(This), as.character(This))
#   This That
# 1   60  679
# 2   90   12
# 3  120  453
# 4   OG  252