I have a data.frame (say "df") looks like this:
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 group "Mortality.Rate" by "State", i.e. order a bid in a certain condition. If there is a connection in speed, then "Hospital.Name" is used to resolve the tie.
The functions "order ()" and "tapply ()" came to my mind. I am encoded as follows:
tapply(df$Mortality.Rate, df$State, order, df$Hospital.Name, na.last=NA)
However, an "argument length" error occurs. When the order function is applied to the sliced Rate, the second order argument (for example, df $ Hospital.Name) is not sliced.
How can I pass the second argument (to resolve the binding in order) to tapply () or are there any other approaches?