如何在R中打破与订单功能的联系

时间:2013-01-18 07:39:46

标签: r sorting

我有一个包含2列的数据框。我已使用order()函数

对它们进行了排序
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

第3和第4个职位是关系(Mortality = 8.7)。我希望在data$Hospital.Name中按字母顺序打破平局,以便“CYPRESS FAIRBANKS”排在第3位,“DETAR HOSPITAL”排在第4位。

1 个答案:

答案 0 :(得分:19)

data$Hospital.Name中使用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