当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。