當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R語言 as.ordered()用法及代碼示例


as.ordered()R語言中的函數將一個無序因子作為參數並將其轉換為有序因子。

用法: as.ordered(factor)

參數:
factor:要轉換的無序因子

範例1:


# Creating a vector 
x<-c("North", "North", "East", "West") 
  
# Converting vector into factor
Directions <- factor(x)
  
# Using as.ordered() Function
# to order an unordered factor
as.ordered(Directions)

輸出:

[1] North North East  West 
Levels:East < North < West

範例2:


# creating vector size 
size = c("small", "large", "large", "small",  
         "medium", "large", "medium", "medium")   
sizes <- ordered(c("small", "large", "large", 
                   "small", "medium")) 
    
# Using as.ordered() Function
# to order an unordered factor
as.ordered(sizes)

輸出:

[1] small  large  large  small  medium
Levels:large < medium < small

相關用法


注:本文由純淨天空篩選整理自nidhi_biet大神的英文原創作品 Convert an Unordered Factor to an Ordered Factor in R Programming – as.ordered() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。