Reputation: 51
I am quite a newbie to R/Programming, and I apologize if this has been asked before, but I cannot find it.
I am using the sort function on a particular column to rank hospitals based on a specific outcome.
The data ranges in value from 8.0 to 20.0.
However, when I use the sort function, instead of the lowest value appearing first, the data will be sorted with 10.0 first, followed by all data beginning with a 1, and will end with the lowest data (8.0, 8.1, 8.2).
For example (though the actual code contains many more values):
10.3 11.5 17.8 19.2 8.1 9.2
I would like the lowest value to be at the top of the list; is there any way I can do this?
(If relevant, my code can be found at: https://github.com/Lalawp/R-Programming/blob/86aac2c5383b5134d9b49eca481cef9c35652815/best.R)
Upvotes: 1
Views: 179
Reputation: 99331
Without your data this is a shot in the dark. But I'm guessing that your column is class character and not numeric. Here's an example
> set.seed(1)
> s <- sample(seq(8, 20, by = 0.1), 20, TRUE)
> sort(s)
[1] 8.7 10.1 10.4 10.4 11.2 12.5 12.5 12.6 14.0 14.9 15.6 15.9
[13] 16.3 16.6 17.3 17.4 18.8 18.9 19.4 20.0
> sort(as.character(s))
[1] "10.1" "10.4" "10.4" "11.2" "12.5" "12.5" "12.6" "14"
[9] "14.9" "15.6" "15.9" "16.3" "16.6" "17.3" "17.4" "18.8"
[17] "18.9" "19.4" "20" "8.7"
You can see that sort
sorts differently for characters and numeric values.
Try changing your column to numeric with df$col <- as.numeric(df$col)
and run your sort again
Upvotes: 1