📜  在 R 编程中将无序因子转换为有序因子 - as.ordered()函数

📅  最后修改于: 2022-05-13 01:55:24.429000             🧑  作者: Mango

在 R 编程中将无序因子转换为有序因子 - as.ordered()函数

R 语言中的as.ordered()函数将无序因子作为参数并将其转换为有序因子。

示例 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