1

I have the following data frame:

sort_data <- data.frame(id=1:10, 
                        var=c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"))

I would like to sort this data in a specific order (using the "id" column) :

sort_order <- c(4, 7, 2, 5, 9, 1, 3, 6, 8, 10)

Is there a quick way to do this in R?

jay.sf
  • 46,523
  • 6
  • 46
  • 87
antonoyaro8
  • 609
  • 8

1 Answers1

2

You could sort by indices as was noted in comments,

sort_data[sort_order, ]
#    id var
# 4   4   D
# 7   7   G
# 2   2   B
# 5   5   E
# 9   9   I
# 1   1   A
# 3   3   C
# 6   6   F
# 8   8   H
# 10 10   J

however, if the IDs don't match the range of indices like so

sort_data1 <- transform(sort_data, id=id + 10)
sort_order1 <- sort_order + 10

your code will break:

sort_data1[sort_order1, ]
#      id  var
# NA   NA <NA>
# NA.1 NA <NA>
# NA.2 NA <NA>
# NA.3 NA <NA>
# NA.4 NA <NA>
# NA.5 NA <NA>
# NA.6 NA <NA>
# NA.7 NA <NA>
# NA.8 NA <NA>
# NA.9 NA <NA>

So it's safer to use match,

sort_data[match(sort_order1, sort_data1$id), ]
#    id var
# 4  14   D
# 7  17   G
# 2  12   B
# 5  15   E
# 9  19   I
# 1  11   A
# 3  13   C
# 6  16   F
# 8  18   H
# 10 20   J

which also works in the first case.

sort_data[match(sort_order, sort_data$id), ]
#    id var
# 4   4   D
# 7   7   G
# 2   2   B
# 5   5   E
# 9   9   I
# 1   1   A
# 3   3   C
# 6   6   F
# 8   8   H
# 10 10   J

If you have missings (i.e. NAs) in your IDs, you may wrap a na.omit(match(.)) around, note however that those IDs will be deleted.

jay.sf
  • 46,523
  • 6
  • 46
  • 87