I have a data frame with 2 columns. I have ordered them using order()
function
data<-data[order(data$Mortality),]
head(data)
Hospital.Name Mortality
FORT DUNCAN MEDICAL CENTER 8.1
TOMBALL REGIONAL MEDICAL CENTER 8.5
DETAR HOSPITAL NAVARRO 8.7
CYPRESS FAIRBANKS MEDICAL CENTER 8.7
MISSION REGIONAL MEDICAL CENTER 8.8
METHODIST HOSPITAL,THE 8.8
3rd and 4th positions are ties (Mortality
= 8.7 for both). I want to break the tie with alphabetical order in data$Hospital.Name
so that "CYPRESS FAIRBANKS" is 3rd and "DETAR HOSPITAL" as 4th.
order() in R The numbers are ordered according to its index by using order(x) . Here the order() will sort the given numbers according to its index in the ascending order.
To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.
Description. order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort. list is the same, using only one argument.
Use data$Hospital.Name
as second argument in order
:
R> data <- data[order(data$Mortality, data$Hospital.Name), ]
R> data
Hospital.Name Mortality
1 FORT DUNCAN MEDICAL CENTER 8.1
2 TOMBALL REGIONAL MEDICAL CENTER 8.5
4 CYPRESS FAIRBANKS MEDICAL CENTER 8.7
3 DETAR HOSPITAL NAVARRO 8.7
6 METHODIST HOSPITAL,THE 8.8
5 MISSION REGIONAL MEDICAL CENTER 8.8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With