R - 阶函数

时间:2014-09-11 06:43:33

标签: r

这是我的数据

                                                              x  i
1                                D W MCMILLAN MEMORIAL HOSPITAL AL
2                                                          <NA> AK
3                           JOHN C LINCOLN DEER VALLEY HOSPITAL AZ
4                             ARKANSAS METHODIST MEDICAL CENTER AR
5                                         SHERMAN OAKS HOSPITAL CA
6                                      SKY RIDGE MEDICAL CENTER CO
7                                       MIDSTATE MEDICAL CENTER CT
8                                                          <NA> DE
9                                                          <NA> DC
10                               SOUTH FLORIDA BAPTIST HOSPITAL FL
11                                UPSON REGIONAL MEDICAL CENTER GA
12                                                         <NA> HI
13                                LOST RIVERS DISTRICT HOSPITAL ID
14 JESSE BROWN VA MEDICAL CENTER - VA CHICAGO HEALTHCARE SYSTEM IL
15                                           COMMUNITY HOSPITAL IN
16                                      COVENANT MEDICAL CENTER IA
17                          COFFEYVILLE REGIONAL MEDICAL CENTER KS
18                             KING'S DAUGHTERS' MEDICAL CENTER KY
19                               NORTH OAKS MEDICAL CENTER, LLC LA
20                                            RUMFORD  HOSPITAL ME
21                                       CIVISTA MEDICAL CENTER MD
22                                             HEYWOOD HOSPITAL MA
23                GENESYS REGIONAL MEDICAL CENTER - HEALTH PARK MI
24                                HEALTHEAST WOODWINDS HOSPITAL MN
25                                      MARION GENERAL HOSPITAL MS
26                                             LIBERTY HOSPITAL MO
27                             FRANCES MAHON DEACONESS HOSPITAL MT
28                             ALEGENT HEALTH MEMORIAL HOSPITAL NE
29                          BANNER CHURCHILL COMMUNITY HOSPITAL NV
30                                   FRANKLIN REGIONAL HOSPITAL NH
31                     CAPITAL HEALTH MEDICAL CENTER - HOPEWELL NJ
32                                            ESPANOLA HOSPITAL NM
33                                 METROPOLITAN HOSPITAL CENTER NY
34                                              MEDWEST HAYWOOD NC
35                                  LISBON AREA HEALTH SERVICES ND
36                                 CINCINNATI VA MEDICAL CENTER OH
37                             JACKSON COUNTY MEMORIAL HOSPITAL OK
38                ST ALPHONSUS MEDICAL CENTER - BAKER CITY, INC OR
39                                               UPMC PASSAVANT PA
40                        HOSPITAL METROPOLITANO DR TITO MATTEI PR
41                                                         <NA> RI
42                                      PALMETTO HEALTH BAPTIST SC
43                            BLACK HILLS SURGICAL HOSPITAL LLP SD
44                                   INDIAN PATH MEDICAL CENTER TN
45                                       NIX HEALTH CARE SYSTEM TX
46                                   BEAR RIVER VALLEY HOSPITAL UT
47                                                         <NA> VT
48                                                         <NA> VI
49                            CARILION GILES COMMUNITY HOSPITAL VA
50                                       SWEDISH MEDICAL CENTER WA
51                                       PLATEAU MEDICAL CENTER WV
52                                         ST CROIX REG MED CTR WI
53                                       POWELL VALLEY HOSPITAL WY
54                                                         <NA> GU

我想按列i排序此列表,但由于某种原因,它会在底部抛出GU。 我跑的时候

order(z$i)

(z是我的桌子) 我得到了这个结果

[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54


> str(z)
'data.frame':   54 obs. of  2 variables:
 $ x: Factor w/ 46 levels "D W MCMILLAN MEMORIAL HOSPITAL",..: 1 NA 2 3 4 5 6 NA NA 7 ...
 $ i: Factor w/ 54 levels "AL","AK","AZ",..: 1 2 3 4 5 6 7 8 9 10 ...

对我来说意味着它认为GU属于列表的底部。此外,列表顶部还有一个问题,AL在AK之前,AZ在AR之前。

有什么建议为什么会这样做?

由于

1 个答案:

答案 0 :(得分:0)

z[order(as.character(z$i)), ]

会做到这一点。