I have a data.frame (say "df") looks like following:
Hospital.Name | State | Mortality.Rate
'hospital_1' | 'AA' | 0.2
'hospital_2' | 'AA' | 0.3
'hospital_3' | 'BB' | 0.3
'hospital_4' | 'CC' | 0.5
(The Hospital.Name is unique)
Now I want to order the "Mortality.Rate" group by "State", i.e. order the rate within a certain state. If there is a tie in the rate, then "Hospital.Name" is used for resolve the tie.
The "order()" and "tapply()" functions came to my mind. I coded like this:
tapply(df$Mortality.Rate, df$State, order, df$Hospital.Name, na.last=NA)
However, an error "argument length differ" popped up. When "order" function is applied to a sliced "Rate", the second argument of order (i.e. df$Hospital.Name) is not sliced.
How could I pass the second argument (for resolution a tie in ordering) to tapply() or is there any other approaches?