0

I am trying to sort a data frame in R by a single column and whatever method I use to sort it, the data is, not sorted, partially sorted, or in a random order and I don't understand why it is not working.

The data frame has a squad name and a finish position, I am trying to sort by finish position

df <- df[order(FinishPos),] 

when running the code above it puts it in the order: 1,10,11,12,13,14,15,16,17,18,19,2,20,3,4,5,6,7,8,9 so it is sorted in parts but not from 1 to 20 like I want

ckhgray
  • 81
  • 1
  • 2
  • 7
  • 2
    Values are character not numeric. Convert them to numeric then sort. Provide example data `dput(mydata)` – zx8754 Feb 27 '23 at 12:35
  • It looks like your finish position is a character value. This might be relevant. https://stackoverflow.com/questions/49084625/sort-strings-by-numbers-inside-them. If you can change your finish position with `as.numeric` you may get the result you are looking for. – Benjamin Feb 27 '23 at 12:35

1 Answers1

0

Your data is likely character (or factor ordered on the string variants of them).

Choices:

  1. Either convert them to integers (or numeric) and sorting will work:

    FinishPos <- as.numeric(FinishPos) # or as.integer
    df[order(FinishPos),]
    
  2. Calc the order based on the numeric conversation (temporary):

    df[order(as.numeric(FinishPos)),]
    
r2evans
  • 141,215
  • 6
  • 77
  • 149