为什么因子()没有取出在R中没有观察的因子的水平

时间:2014-01-27 02:56:06

标签: r r-factor

任何人都可以告诉我这怎么可能?

> levels(x$Species)
 [1] "alisterus amboinensis"        "amazona viridigenalis"        "aprosmictus erythropterus"   
 [4] "ara macao"                    "aratinga pertinax"            "bolbopsittacus lunulatus"    
 [7] "brotogeris jugularis"         "cacatua sulphurea"            "charmosyna papou"            
[10] "coracopsis vasa"              "cyanoliseus patagonus"        "cyanoramphus auriceps"       
[13] "cyclopsitta diophthalma"      "deroptyus accipitrinus"       "diopsittaca nobilis"         
[16] "eclectus roratus"             "enicognathus leptorhynchus"   "eos reticulata"              
[19] "forpus passerinus"            "geoffroyus heteroclitus"      "guaruba guarouba"            
[22] "hapalopsittaca amazonina"     "leptosittaca branickii"       "loriculus galgulus"          
[25] "lorius albidinuchus"          "melopsittacus undulatus"      "micropsitta finschii"        
[28] "myiopsitta monachus"          "nandayus nenday"              "nannopsittaca panychlora"    
[31] "neopsephotus bourkii"         "neopsittacus musschenbroekii" "nestor notabilis"            
[34] "northiella haematogaster"     "orthopsittaca manilata"       "phigys solitarius"           
[37] "pionites melanocephala"       "pionopsitta caica"            "pionus menstruus"            
[40] "platycercus adscitus"         "poicephalus robustus"         "polytelis alexandrae"        
[43] "prioniturus luconensis"       "prosopeia tabuensis"          "pseudeos fuscata"            
[46] "psittacula columboides"       "psittaculirostris edwardsii"  "psittacus erithacus"         
[49] "psittrichas fulgidus"         "purpureicephalus spurius"     "pyrrhura picta"              
[52] "rhynchopsitta pachyrhyncha"   "tanygnathus lucionensis"      "touit batavica"              
[55] "vini australis"              
> x[x$species=="vini australis",]
[1] Species   Longitude Latitude 
<0 rows> (or 0-length row.names)
> x$Species=factor(x$Species)
> levels(x$Species)
 [1] "alisterus amboinensis"        "amazona viridigenalis"        "aprosmictus erythropterus"   
 [4] "ara macao"                    "aratinga pertinax"            "bolbopsittacus lunulatus"    
 [7] "brotogeris jugularis"         "cacatua sulphurea"            "charmosyna papou"            
[10] "coracopsis vasa"              "cyanoliseus patagonus"        "cyanoramphus auriceps"       
[13] "cyclopsitta diophthalma"      "deroptyus accipitrinus"       "diopsittaca nobilis"         
[16] "eclectus roratus"             "enicognathus leptorhynchus"   "eos reticulata"              
[19] "forpus passerinus"            "geoffroyus heteroclitus"      "guaruba guarouba"            
[22] "hapalopsittaca amazonina"     "leptosittaca branickii"       "loriculus galgulus"          
[25] "lorius albidinuchus"          "melopsittacus undulatus"      "micropsitta finschii"        
[28] "myiopsitta monachus"          "nandayus nenday"              "nannopsittaca panychlora"    
[31] "neopsephotus bourkii"         "neopsittacus musschenbroekii" "nestor notabilis"            
[34] "northiella haematogaster"     "orthopsittaca manilata"       "phigys solitarius"           
[37] "pionites melanocephala"       "pionopsitta caica"            "pionus menstruus"            
[40] "platycercus adscitus"         "poicephalus robustus"         "polytelis alexandrae"        
[43] "prioniturus luconensis"       "prosopeia tabuensis"          "pseudeos fuscata"            
[46] "psittacula columboides"       "psittaculirostris edwardsii"  "psittacus erithacus"         
[49] "psittrichas fulgidus"         "purpureicephalus spurius"     "pyrrhura picta"              
[52] "rhynchopsitta pachyrhyncha"   "tanygnathus lucionensis"      "touit batavica"              
[55] "vini australis"              
> x[x$species=="vini australis",]
[1] Species   Longitude Latitude 
<0 rows> (or 0-length row.names)

1 个答案:

答案 0 :(得分:3)

您正在寻找功能droplevels。它从一个因子中删除未使用的级别。

如您所见,默认情况下不会删除级别。您可能想知道有些值没有观察值。

以下是一个例子:

(x <- factor(LETTERS))
##  [1] A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
## Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

现在选择一个子集:

(y <- x[10:15])
## [1] J K L M N O
## Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

降低等级会导致:

(z <- droplevels(y))
## [1] J K L M N O
## Levels: J K L M N O

不再使用的级别不再存在。如果您使用因子的数值,请小心,因为droplevels更改了表示形式:

as.numeric(y)
## [1] 10 11 12 13 14 15

as.numeric(z)
## [1] 1 2 3 4 5 6