2016-07-23 70 views
3

這是我從互聯網加載的數據幀的一部分使用readHTMLtable訂單因子水平,其中所述水平顯示在數據

head(tt,59) 
    year   sport      event  athlete_id medal 
1 1896 Track & Field     100m Men  BURKETOM01 GOLD 
2 1896 Track & Field     100m Men  HOFMAFRI01 SILVER 
3 1896 Track & Field     100m Men  LANEFRA01 BRONZE 
4 1896 Track & Field     100m Men  SZOKOALA01 BRONZE 
5 1896 Track & Field     400m Men  BURKETOM01 GOLD 
6 1896 Track & Field     400m Men  JAMISHER01 SILVER 
7 1896 Track & Field     400m Men  GMELICHA01 BRONZE 
8 1896 Track & Field     800m Men  FLACKTED01 GOLD 
9 1896 Track & Field     800m Men D<C1>NIN<C1>N01 SILVER 
10 1896 Track & Field     800m Men  GOLEMDEM01 BRONZE 
11 1896 Track & Field     1500m Men  FLACKTED01 GOLD 
12 1896 Track & Field     1500m Men  BLAKEART01 SILVER 
13 1896 Track & Field     1500m Men  LERMUALB01 BRONZE 
14 1896 Track & Field    Marathon Men  LOUISSPI01 GOLD 
15 1896 Track & Field    Marathon Men  VASILCHA01 SILVER 
16 1896 Track & Field    Marathon Men  KELLNGYU01 BRONZE 
17 1896 Track & Field   110m Hurdles Men  CURTITOM01 GOLD 
18 1896 Track & Field   110m Hurdles Men  GOULDGRA01 SILVER 
19 1896 Track & Field    High Jump Men  CLARKELL01 GOLD 
20 1896 Track & Field    High Jump Men  CONNOJAM01 SILVER 
21 1896 Track & Field    High Jump Men  GARREBOB01 SILVER 
22 1896 Track & Field    Pole Vault Men  HOYTBIL01 GOLD 
23 1896 Track & Field    Pole Vault Men  TYLERALB01 SILVER 
24 1896 Track & Field    Pole Vault Men  THEODIOA01 BRONZE 
25 1896 Track & Field    Pole Vault Men  DAMASEVA01 BRONZE 
26 1896 Track & Field    Long Jump Men  CLARKELL01 GOLD 
27 1896 Track & Field    Long Jump Men  GARREBOB01 SILVER 
28 1896 Track & Field    Long Jump Men  CONNOJAM01 BRONZE 
29 1896 Track & Field   Triple Jump Men  CONNOJAM01 GOLD 
30 1896 Track & Field   Triple Jump Men TUFF<C8>ALE01 SILVER 
31 1896 Track & Field   Triple Jump Men  PERSAIOA01 BRONZE 
32 1896 Track & Field    Shot Put Men  GARREBOB01 GOLD 
33 1896 Track & Field    Shot Put Men  GOUSKMIL01 SILVER 
34 1896 Track & Field    Shot Put Men  PAPASGEO01 BRONZE 
35 1896 Track & Field   Discus Throw Men  GARREBOB01 GOLD 
36 1896 Track & Field   Discus Throw Men  PARASPAN01 SILVER 
37 1896 Track & Field   Discus Throw Men  VERSISOT01 BRONZE 
38 1896  Cycling 2000m Sprint (Scratch) Men  MASSOPAU01 GOLD 
39 1896  Cycling 2000m Sprint (Scratch) Men  NIKOLSTA01 SILVER 
40 1896  Cycling 2000m Sprint (Scratch) Men FLAMEL<C9>O01 BRONZE 
41 1896  Cycling Individual Road Race Men  KONSTARI01 GOLD 
42 1896  Cycling Individual Road Race Men  GOEDRAUG01 SILVER 
43 1896  Cycling Individual Road Race Men  BATTEEDW01 BRONZE 
44 1896  Cycling    One-Lap Race  MASSOPAU01 GOLD 
45 1896  Cycling    One-Lap Race  NIKOLSTA01 SILVER 
46 1896  Cycling    One-Lap Race  SCHMAADO01 BRONZE 
47 1896  Cycling   10km Track Race  MASSOPAU01 GOLD 
48 1896  Cycling   10km Track Race FLAMEL<C9>O01 SILVER 
49 1896  Cycling   10km Track Race  SCHMAADO01 BRONZE 
50 1896  Cycling   100km Track Race FLAMEL<C9>O01 GOLD 
51 1896  Cycling   100km Track Race  KOLETGEO01 SILVER 
52 1896  Cycling    12-Hour Race  SCHMAADO01 GOLD 
53 1896  Cycling    12-Hour Race  KEEPIFRA01 SILVER 
54 1896  Fencing   Foil, Individual  GRAVEEUG01 GOLD 
55 1896  Fencing   Foil, Individual  CALLOHEN01 SILVER 
56 1896  Fencing   Foil, Individual  PIERRPER01 BRONZE 
57 1896  Fencing   Sabre, Individual  GEORGIOA01 GOLD 
58 1896  Fencing   Sabre, Individual  KARAKTEL01 SILVER 
59 1896  Fencing   Sabre, Individual  NIELSHOL01 BRONZE 

正如你可以看到變量sport是一個因素。當我檢查的水平,這是我所得到的:

levels(tt$sport) 
[1] "Cycling"  "Fencing"  "Gymnastics" "Shooting"  "Swimming"  "Tennis" 
[7] "Track & Field" "Weightlifting" "Wrestling 

出於某種原因,其中水平出現不匹配的數據幀順序的順序。我正在尋找一種方式,其中使用水平的功能會給我根據第一次亮相組織級別的列表,類似的東西:

levels(medals.df$tt) 
[1] "Track & Field" "Cycling"  "Fencing"  "Gymnastics" "Shooting" "Swimming" 
[7] "Tennis"  "Weightlifting" "Wrestling" 

現在,另一件事要記住的是,列運動是而不是「塊設計」,這意味着前59行具有相同的相鄰值,但在整個數據框中不是這樣。

回答

1

請注意,我必須調整您的數據集,以便您列出的所有級別出現,並按照您指定的順序進行。從那裏,我寫了一個簡單的函數,按照它們出現在數據集中的順序輸出這些級別。關鍵是要使用which(其中列出符合標準的觀察行數),min(選擇最低值)和order(它告訴您使用的順序以從最低到最高)。

d <- read.table(text="rn year sport   event  athlete_id medal 
1 1896 'Track & Field'     '100m Men'  'BURKETOM01' 'GOLD' 
53 1896  'Cycling'    '12-Hour Race'  'KEEPIFRA01' 'SILVER' 
54 1896  'Fencing'   'Foil, Individual'  'GRAVEEUG01' 'GOLD' 
55 1896  'Gymnastics'   'Foil, Individual'  'CALLOHEN01' 'SILVER' 
56 1896  'Shooting'   'Foil, Individual'  'PIERRPER01' 'BRONZE' 
57 1896  'Swimming'   'Sabre, Individual'  'GEORGIOA01' 'GOLD' 
58 1896  'Tennis'   'Sabre, Individual'  'KARAKTEL01' 'SILVER' 
58 1896  'Weightlifting'   'Sabre, Individual'  'KARAKTEL01' 'SILVER' 
59 1896  'Wrestling'   'Sabre, Individual'  'NIELSHOL01' 'BRONZE'", 
       header=T) 

levels(d$sport) 
# [1] "Cycling"  "Fencing"  "Gymnastics" "Shooting"  
# [5] "Swimming"  "Tennis"  "Track & Field" "Weightlifting" 
# [9] "Wrestling"  

level.order <- function(var){ 
    l <- levels(var) 
    o <- c() 
    for(i in 1:length(l)){ 
    o[i] <- min(which(var==l[i])) 
    } 
    return(l[order(o)]) 
} 
level.order(d$sport) 
# [1] "Track & Field" "Cycling"  "Fencing"  "Gymnastics" 
# [5] "Shooting"  "Swimming"  "Tennis"  "Weightlifting" 
# [9] "Wrestling"  

從這裏,如果你想改變默認的排序(按字母順序排列)的水平在數據集中顯示的順序,你會使用factor。試想一下:

levels(d$sport) 
# [1] "Cycling"  "Fencing"  "Gymnastics" "Shooting"  
# [5] "Swimming"  "Tennis"  "Track & Field" "Weightlifting" 
# [9] "Wrestling"  
d$sport <- factor(d$sport, levels=level.order(d$sport)) 
levels(d$sport) 
# [1] "Track & Field" "Cycling"  "Fencing"  "Gymnastics" 
# [5] "Shooting"  "Swimming"  "Tennis"  "Weightlifting" 
# [9] "Wrestling"  
+2

相反的'level.order()'函數你也可以使用:'d $ sport < - factor(d $ sport,levels = unique(d $ sport))''。 –

+0

不錯的一點,@KenS。我不知道'unique()'總是按照它們出現的順序列出這些值。你爲什麼不作出正式答案? – gung

+0

它的工作,謝謝 – Lee

2

我使用的數據幀@gung在他的回答設置:

d <- read.table(text="rn year sport   event  athlete_id medal 
1 1896 'Track & Field'     '100m Men'  'BURKETOM01' 'GOLD' 
53 1896  'Cycling'    '12-Hour Race'  'KEEPIFRA01' 'SILVER' 
54 1896  'Fencing'   'Foil, Individual'  'GRAVEEUG01' 'GOLD' 
55 1896  'Gymnastics'   'Foil, Individual'  'CALLOHEN01' 'SILVER' 
56 1896  'Shooting'   'Foil, Individual'  'PIERRPER01' 'BRONZE' 
57 1896  'Swimming'   'Sabre, Individual'  'GEORGIOA01' 'GOLD' 
58 1896  'Tennis'   'Sabre, Individual'  'KARAKTEL01' 'SILVER' 
58 1896  'Weightlifting'   'Sabre, Individual'  'KARAKTEL01' 'SILVER' 
59 1896  'Wrestling'   'Sabre, Individual'  'NIELSHOL01' 'BRONZE'", 
      header=T) 

levels(d$sport) 

然後你可以使用unique(d$sport)中的影響因子函數是這樣的:

d$sport <- factor(d$sport, levels=unique(d$sport)) 
# Check the results: 
levels(d$sport)